Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What effect has $ in a Java class name

Tags:

java

class

gson

I recently saw the source code of the Google library Gson and saw something really strange (IMO). In the com.google.gson.internal package there are 2 classes which do begin with a "$".

For example

public final class $Gson$Types { ... }

Does the dollar sign has a effect to development or is it just to say to outside developers something like "please don't use cause it is a internal class"?

like image 809
OemerA Avatar asked Sep 20 '12 15:09

OemerA


1 Answers

You can use any currency sign or continuation character in an identifier.

By using $, it suggests it is generated e.g. generated by the build, or for internal use only.

Uses for special characters in Java code

Hidden code

It only causes a problem if you have conflicts with generated classes e.g.

class A {
     class B {
     }
}
class A$B {
}

reports

error: duplicate class: A.B

Compiler generated classes do not start with a $ which may be reason this as done.

On Windows 7, the special characters which can appear in a Class are

public static void main(String... args) throws IOException {
    for (char ch = 0; ch < Character.MAX_VALUE; ch++) {
        if (Character.isJavaIdentifierStart(ch) && !Character.isLetter(ch))
            System.out.println("interface " + ch + " { }");
    }
}

interface $ { }
interface _ { }
interface ¢ { }
interface £ { }
interface ¤ { }
interface ¥ { }
interface € { }

Other special characters are allows by Java, but not supported as file names. This may depend on the OS and possibly the language.

like image 118
Peter Lawrey Avatar answered Oct 12 '22 06:10

Peter Lawrey