Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question

 
Dean Oso
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?

thanks,
Dean
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
ex.
$5gf
_fd_
f54
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at what JLS says
Here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic