• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what makes a legal identifier?

 
YekSoon Lok
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am preparing for my SCJP and I just make a small observation
for "what makes a legal identifier?"
if you have previous experience in C++ and some form of Unix scripting (like Perl), you will know that
1. in C++, identifiers can start with "_" , the underscore
2. in Perl, identifiers start with "$", dollar sign.
and of course, most of you knows that identifiers can start with
a character.
Now, combine the 3 and u have a guideline for "what makes a legal identifier" or "what is a legal declaration?"
1. can start with characters
2. can start with underscore
3. can start with dollar sign
anything outside of this will not be correct?
is this observation right?
thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
In addition, no special characters (for example *, @) can appear anywhere in a legal identifier.
for example, _addd@ will not be accepted.
[This message has been edited by Shyamsundar Gururaj (edited November 29, 2001).]
 
Jim Hall
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't forget the obvious. An identifier cannot be a keyword or reserved word.
Also an identifier can be the same name as the class or methods in the class without causing a compile error. Of course you probably wouldn't want to do this.
<code><pre>
public class Test
{
public static void main(String[] args)
{
int Test = 1;
System.out.println(Test);
int amethod = 2;
System.out.println(amethod);
}
public void amethod(){}
}
</pre>
</code>

[This message has been edited by Jim Hall (edited November 30, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic