aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes identifier 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 "identifier" Watch "identifier" New topic
Author

identifier

Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127

Why does this code even compile? Isn't String a keyword? Isn't it true that a keyword can't be used as an identifier?
thanks,
Jenny
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jenny Yin:
Isn't String a keyword? Isn't it true that a keyword can't be used as an identifier?
thanks,
Jenny

String is not a keyword - it's the name of a class. Class names are still legal identifiers in Java. It is true, however, that keywords can't be used as identifiers. For example, you can't use "this" as an identifier.
Corey
[ April 24, 2002: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Jenny,
In the code you gave, the local String variable effectively shadows the java.lang.String class. Consider the following code:

This code compiles and produces the output:
footest
5
On line 7, you need to fully qualify the reference to java.lang.String otherwise you will get a compiler error because the compiler will use the local String variable.
Notice the compiler is smart enough (well, actually the guys who wrote the compiler are the smarties) to use the context to recognize that 'String' on line 4 refers to the shadowed java.lang.String rather than the local variable String.
HTH,
Junilu
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Corey,
Thanks for pointing that out.
Junilu,
That's a fun exercise you showed me. Thanks.
-Jenny
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: identifier