• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Main and inner class naming.

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not experiment?



Put this in a file and see if it compiles.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 (
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 +-*/()[]=<>?:&^~|'"/!{};,.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic