Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class name containing dollar sign fails to compile if an inner class is present

Tags:

java

I've got following Java classes defined:

mac-grek:javajunk grek$ cat A\$B.java
class A$B {}
mac-grek:javajunk grek$ cat A.java
public class A {
  public static class B {}
}
mac-grek:javajunk grek$ cat Main.java 
public class Main {

  public static void main(String[] args) {
    System.out.println(A.B.class.getName());
    System.out.println(A$B.class.getName());
  }

}

When I try to compile them, I get following errors:

mac-grek:javajunk grek$ javac 'A$B.java' A.java Main.java
A.java:2: duplicate class: A.B
  public static class B {}
                ^
Main.java:4: cannot find symbol
symbol  : class B
location: class A
    System.out.println(A.B.class.getName());
                        ^
Main.java:5: cannot find symbol
symbol  : class A$B
location: class Main
    System.out.println(A$B.class.getName());
                       ^
3 errors

If I remove A.java file and System.out.println(A.B.class.getName()); from Main.java everything compiles:

mac-grek:javajunk grek$ cat A\$B.java 
class A$B {}
mac-grek:javajunk grek$ cat Main.java 
public class Main {

  public static void main(String[] args) {
    System.out.println(A$B.class.getName());
  }

}
mac-grek:javajunk grek$ javac A\$B.java Main.java
mac-grek:javajunk grek$ 

So Java allows me to define a class containing dollar sign in it's name. How can I compile my original example?

like image 777
Grzegorz Kossakowski Avatar asked May 29 '11 10:05

Grzegorz Kossakowski


People also ask

Can Java use dollar in class name?

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed. Variable names should be short yet meaningful.

Is dollar sign allowed in Java?

"Java does allow the dollar sign symbol $ to appear in an identifier, but these identifiers have a special meaning, so you should not use the $ symbol in your identifiers."

What is '$' in Java?

First, in Java '$' is a valid symbol for variable names and you can just use it for whatever you like: making your code cla$$ier, denoting that variables are private under a code convention, etc. Second, '$' is used as a separator character for the Java compiler to specify when a class is declared under another class.

How do you declare an inner class in Java?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.


1 Answers

You have a name conflict because you defined a top-level class A$B having the same name as the generated name for a static inner class B of class A. Since you have both, the compiler can't resolve the conflict.

The JLS says:

The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

Since you decided not to respect that rule, you got bitten by javac. I would just rename A$B to something else.

like image 77
JB Nizet Avatar answered Oct 10 '22 11:10

JB Nizet