It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Simple question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Simple question" Watch "Simple question" New topic

Simple question

Dean Oso

Joined: Jul 13, 2003
Posts: 9
can somebody tell me the rules for a valid varible or method name? I have Kathy Sierra & Bert Bates book but havent seen it documented in there?

Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
Actually, they have it in there, but not in the actual text, why I don't know. It took me a while to find as well.
Anyway, you can find it in the glossary, but I'll just tell you the rules.
All method/var names must begin with either a letter, $, or _ (underscore) and the rest of the var/method name can only contain letter, numbers, $ or _ It does not matter if you use upper or lowercase but you must be consistent as Java is case sensitive.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
In real life you may need to know that an identifier is a sequence of Java letters and Java digits, the first of which must be a Java letter.
If something is considered a letter or digit in a human language, you can probably use it in identifiers. This allows programmers to use identifiers in their programs that are written in their native languages.
The currency symbols $, pound sign ('\u00a3') and yen sign ('\u00a5') are Java letters. See Character.isJavaIdentifierStart(char) and Character.isJavaIdentifierPart(char).
Java letters from the first 128 Unicode characters are $ A-Z _ a-z

[ July 13, 2003: Message edited by: Marlene Miller ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Dean
I guess that you are already aware of this but justincluding it for the sake of completeness
Reserved words and keywords and true,false and null(constants) can not also be used as a valid identifier.
La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 161
Have a look at what JLS says

La Vish
SCJP 1.4, President 60s Club
I agree. Here's the link:
subject: Simple question
It's not a secret anymore!