wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class name's as Identifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class name Watch "Class name New topic
Author

Class name's as Identifiers

Mohan Mani
Greenhorn

Joined: Sep 02, 2004
Posts: 16
Hi all

Im just having a query on the below post.

Which of the following are not legal Java identifiers?
Select 1 correct option.
a goto
Although not used, it's a reserved word.
b unsigned
It's not a reserved or keyword.
c String
Yes, it is valid. This is a valid statement: String String = "String";
d _xyz
Can start with _ or $
e $_abc
Can have _ or $.

Ans a.
why not b??


In the option C, a class name "String" is used as an identifer. I had tried with others like Integer and code compiles. How java allows this.

Any specific reason why class name are been allowed to be used as identifiers.

Regards
Mohan
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Essentially, identifiers (e.g., class names) are up to the programmer, while keywords are fixed as part of the Language Specification.

6.1: Declarations...
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#33757

3.8: Identifiers...
http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#40625

3.9 Keywords...
http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#229308


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Marc,

So does the JVM treat an indentifier that has the same name as a class as an indentifier or a class?




Output:

AClass.aMethod()

But why does it choose that (selecting the method by using the ACLass type reference that BCLass identifier is assigned to) rather than the static method on BClass?

Oooh - my brain aches now...

Cheers,

Si.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Simon Cockayne:
...does the JVM treat an indentifier that has the same name as a class as an indentifier or a class? ...

Good question!

See JLS 6.5: Determining the Meaning of a Name...
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#106941

Basically, the qualified name "BClass.aMethod()" is identified by its context as a MethodName, and is initially classified as AmbiguousName.Identifier.

The AmbiguousName (as a simple identifier) is then reclassified as an ExpressionName (rather than a TypeName) per section 6.5.2.

But I think the point here -- as above -- is that it's up to the programmer to avoid this type of confusion.
[ March 30, 2005: Message edited by: marc weber ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Another way to think about this is that Java has several namespaces and the same isentifier can have a different meaning in each namespace.

For example, C can be a class name, a variable name, and a label name (used in break and continue), all at the same time. The Java compiler always knows which identifier dictionary to look in depending on the statement syntax.


Mike Gershman
SCJP 1.4, SCWCD in process
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mike Gershman:
Another way to think about this is that Java has several namespaces and the same identifier can have a different meaning in each namespace...

Exactly. In the above example, the AmbiguousName is reclassified as an ExpressionName because that variable is in scope.

Specifically (per 6.5.2), "the AmbiguousName is a simple name, consisting of a single Identifier, [and] ... the Identifier appears within the scope ... of a local variable declaration..."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class name's as Identifiers
 
Similar Threads
Primatives and Literals
identifier
pgjc - mock exam questions (answers incorrect)?
legal java identifiers versus not a keyword/reserved. whats the difference
Style Guide Question