• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Class name's as Identifiers

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..."
reply
    Bookmark Topic Watch Topic
  • New Topic