18

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?

2

3 Answers 3

34

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.

2
  • 1
    This rule is very vague. Why javac would care if my code is coming from some generator or written by hand? My question tries to explore this simple question: what's valid name for a class in Java? It seems to me that JLS is quite leaky about that but still I appreciate pointing me to relevant fragment of JLS. I'll accept this answer in a case there's no more precise answer to my question. May 29, 2011 at 11:09
  • @GrzegorzKossakowski - No such rule is possible. Uses of $ can vary from one version of Java to the next. Other uses may come from 3rd party software.
    – Stephen C
    Oct 18, 2011 at 22:36
12

This rule is very vague.

I disagree. To me, it says "don't do it ... unless you know what you are doing". It doesn't say why, but it doesn't need to. Indeed, it can't fully explain why because some of the uses of '$' in identifiers could come from 3rd-party software.

Why javac would care if my code is coming from some generator or written by hand?

It doesn't "care". But on the other hand it does assume that you know what you are doing if you choose to ignore the JLS advice.

The point is that people who write generators are expected to know that the binary class names for inner classes are represented using the '$' character. Other people should simply follow the advice in the JLS.

The reason that the Java Language Specification does not set out how the JVM uses '$' is "separation of concerns". From the JLS perspective, this is just an implementation detail. Indeed, it is conceivable that someone will implement the Java language for a virtual machine platform that treats inner classes differently.


I'd like to know precisely in which cases I can use $ in names of my classes.

It is not possible to answer that definitively. And it is probably a good thing that it is not possible; see below.

JLS should be very precise and formal and should not refer to reader's mental state when interpreting it's statements.

  1. There are some areas where it would be bad for the JLS to be formal and precise. This is one of them. For example, if they declared that '$' could safely if you followed rules X, Y and Z, this would limit their possible uses of '$' in future versions of Java. Changing the rules about what identifiers are workable could cause major source-code compatibility head-aches.

    (Other areas where this principle applies are the Memory Model and the semantics of garbage collection.)

  2. The JLS does not refer to your mental state. Those were my words.

2
  • 1
    Thanks Stephen for your answer. I know that SO is not a place designed for discussion so let me make my point clear: I'd like to know precisely in which cases I can use $ in names of my classes. JLS should be very precise and formal and should not refer to reader's mental state when interpreting it's statements. I'm still giving some chance for a person to come up with precise rule. Otherwise, I'll accept JB Nizet's answer which is probably the best one can get. May 29, 2011 at 17:43
  • see Strange.java by Robert Sedgewick as there are several of these issues though nowhere near as bite-you as this one with the result of knowing how the issues affect coding practices reveals several good coding practices Nov 23, 2011 at 8:40
2

Both your class class A$B and your static class B shares the same. Compiler generates OuterClassName$InnerClassName as class name for nested classes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.