• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main and inner class naming.

 
William Wild
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Long time since I had to ask such a simple question, but ....

What is the legal Java class naming convension for main and inner classes?

I know it should be AxxxBxxx ( Camel Humpped starting with a letter ) but what is actually legal to the compiler? Are all of the follwoing legal?

AxxBaa
Axx_Baa
_AxxBxx
_0AxxBaa

Answers would be greatfully appreciated.

Cheers

Bill
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not experiment?



Put this in a file and see if it compiles.
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William,

Briefly, a class name can be an unlimited series of Unicode characters that begins with a letter.

You may read a detailed answer Here

Best regards,
 
William Wild
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically any [A-Z][a-z,A-Z,_]* is ok.

But is the '_' ok at the start?

I would test but haven't got access to a Java Compiler at the mo.

Cheers

Bill

Originally posted by Lionel Badiou:
Hi William,

Briefly, a class name can be an unlimited series of Unicode characters that begins with a letter.

You may read a detailed answer Here

Best regards,
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Wild:
So basically any [A-Z][a-z,A-Z,_]* is ok.

But is the '_' ok at the start?

I would test but haven't got access to a Java Compiler at the mo.

Cheers

Bill


AFAIK, '_' isn't a letter, so according to Lionel's explanation above, you cannot start any identifier name with it. Also, your regex does describe SOME possible indentifier names, but other characters are allowed as well. In particular, I was surprised when I learned that '$' is valid in a name. (For some reason I think it is allowed at the beginning, too, but check the link Lionel gave to check.) It also makes sense that Java allows other Unicode characters as well. This allows programmers that don't speak English as their native language to use names from their own language, including those that have characters not available in the English alphabet. So I think you should read the link above. It says the rules are for variable names, but it still applies to classes and methods as well.

Layne
 
William Wild
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, I know the reason behind the '$'.

It is used to delimit innerclasses when compiling code. Inner classes are compiled to separate class output files in the form OuterClass$InnerClass.class, so if the compiler can build in references to classes using '$' you can write one with the $ in the name.

It's about the only proper rule I know (
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only limitation on identifiers is that the first character can't be a digit. The other characters can be just about anything that is printable in some language and is not used in Java syntax like +-*/()[]=<>?:&^~|'"/!{};,.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic