Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any risk using a single dollar sign `$` as a java class name?

Tags:

java

java-8

javac

Originally I was using the underscore _ as a class name. The new Java8 compiler complains that it "might not be supported after Java SE 8". I changed that to $, and there is no warning any more. However I remember that $ is used by Java to indicate an inner/embedded class in the byte code. I am wondering if there is any risk to use a dollar sign $ as a class name

Some background to this question. What I want to do is to overcome the fact that Java doesn't support pure function, and the _ or $ is to put an namespace to encapsulate some very generic concept (classes/static methods). and neither do I have a good name for this, nor do I want the lib user type too many things to reference that namespace. Here is the code showing what I am doing under the way: https://github.com/greenlaw110/java-tool/blob/master/src/main/java/org/osgl/_.java

like image 682
Gelin Luo Avatar asked Oct 22 '13 22:10

Gelin Luo


People also ask

Can a class name start with dollar sign in Java?

A class name is an identifier—a series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) that does not begin with a digit and does not contain spaces. Some valid identifiers are Welcome1 , $value , _value , m_inputField1 and button7 .

Can Java use dollar sign?

"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 Java class $1?

The $1 are anonymous inner classes you defined in your WelcomeApplet. java file. e.g. compiling public class Run { public static void main(String[] args) { System.out.println(new Object() { public String toString() { return "77"; } }); } private class innerNamed { } }

What is $$ in Java?

So $ is used to separate names for inner classes, and $$ marks lambda-expression-based inner classes.


1 Answers

It is bad style, and potentially risky to use $ in any identifier in Java. The reason it is risky is that the $ character is reserved for the use of the Java toolchain and third-party language tools.

  • It is used by Java compilers in "internal" class names for inner and nested classes.
  • It is used by Java compilers in the names of synthetic attributes.
  • It could be used by third-party code generators (e.g. annotation processors) for various purposes.
  • It could be used by other languages that target the JVM platform, and that might need to co-exist with your code.

You probably won't have technical issues with a plain $ classname at the moment (at least with respect to the standard Java toolchain). But there's always the possibility that this will change in the future:

  • They have (effectively) reserved the right to change this1.
  • There is a precedent for doing this in the _ example.

If you really, really need a one-character classname, it would be better to play it safe and use F or Z or something else that isn't reserved.

But to be honest, I think you'd be better off trying to implement (or just use) a real functional language than trying to shoe-horn a functional programming "system" into Java. Or maybe, just switch to Java 8 ahead of its official release. 'Cos I for one would refuse to read / maintain a Java codebase that looked like jquery.


I don't mean to create a functional lib for Java, just want to create a lib to maintain some common utilities I used. Again, I am a advocate of minimalism and feel suck with things like apache commons. The functional stuff is added to help me easier to manipulate collection(s).

If it is your code, you can do what you like. Make your own decisions. Act on your opinions. Be a "risk taker" ... :-). (Our advice on $, etcetera ... is moot.)

But if you are writing this code for a client or employer, or with the intention of creating a (viable) open source product, then you need to take account of other people's opinion. For example, your boss needs to have an informed opinion on how maintainable your code will be if you find a better paying job somewhere else. In general, will the next guy be able to figure it out, keep your code, fresh, etc ... or will it be consigned to the dustbin?


1 - JLS §3.8 states "The $ character should be used only in mechanically generated source code". That is saying "use it at your peril". The assumption is that folks who build their own source code generators can change them if the standard toolchain uses a bare $ ... but it is harder to change lots of hand written code, and that would be an impediment to upgrading.

like image 134
Stephen C Avatar answered Oct 16 '22 11:10

Stephen C