aspose file tools*
The moose likes Beginning Java and the fly likes Main and inner class naming. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Main and inner class naming." Watch "Main and inner class naming." New topic
Author

Main and inner class naming.

William Wild
Greenhorn

Joined: Dec 10, 2002
Posts: 23
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


--<p>Bill<p>"Make it idiot proof,<br /> and someone will make a better idiot" -- ANON
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

Why not experiment?



Put this in a file and see if it compiles.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
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,


Lionel Badiou
CodeFutures Software
William Wild
Greenhorn

Joined: Dec 10, 2002
Posts: 23
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
William Wild
Greenhorn

Joined: Dec 10, 2002
Posts: 23
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

Joined: Mar 13, 2004
Posts: 1272
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 +-*/()[]=<>?:&^~|'"/!{};,.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main and inner class naming.