wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is this OK! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is this OK!" Watch "Why is this OK!" New topic
Author

Why is this OK!

Abbu Rahman
Ranch Hand

Joined: Jan 05, 2004
Posts: 46
Hello,
I need to know whats happening in the code below, why does javac allow us to make vars with name names like String, System, Class ... i mean the imported class ...
why is there no conflict while making ref of String,Class.. ?
why is javac complaining while using static methods of String,Class.. ??

whats happening here ???
Rafay.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Those words "Class", "System", "String" are not keywords. So its totally legal to use them as variables if you want.
But I wouldn't recommend it --
Not only does it really confuse someone who's trying to use your code later -- but you could defintely goof up your application from naming conflicts.


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
In Java, you can use any string that isn't considered a keyword or a literal as an identifier. You can find a list of the keywords in the JLS, §3.8 Identifiers. The keywords include things like: for, break, while, and throw. The literals include true, false, and null. From that section, we get this quote:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword (�3.9), boolean literal (�3.10.3), or the null literal (�3.10.7).

They even give an example identifier as being the word, "String."
This is legal, but dangerous. When I write the following line of code:

I have now "hidden" the normal meaning of String. Rather than referring to java.lang.String, the compiler will assume that any reference to String means the local int variable. Therefore, in the next line of code:

The java compiler is going to try to invoke the valueOf method of an int primitive! Obviously, no primitive has such a method (or any method, for that matter) and you get a compiler error.
If you wanted the next line of code to run properly after doing so, you'd have to write it like this:

Now, the compiler knows that you're talking about the object named String, not the int named String.
In Java, you can make a class any name you'd like. Therefore, you might often use a variable named i as a counter in loops. There is nothing stopping someone from writing a class named i. If Java prevented you from using class names as variable names, once someone wrote that class, you'd never be able to use i as a counter again!
You can use just about any string as an identifier, but you need to be especially careful when using identifiers that are the same as well known classes or you'll run into this problem.
I hope that helps,
Corey


SCJP Tipline, etc.
Abbu Rahman
Ranch Hand

Joined: Jan 05, 2004
Posts: 46
Thnks for that instant reply (Iam new to JavaRanch).
but what i wanted to know is .. why is javac allowing ref to be made
(in the above code) and why using static methods are creating problem.
i got a question in JQPlus ... it just said
String String ="ere"; //OK
no explanation ...
i'd be happy if you just give me a link about this topic
in JLS or tell me the chapter name in Khalid's book.
thanks again.
Rafay.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm not really sure what you're asking for, then. It seems to me, Jess and I answered your question (and there is a link to the appropriate section of the JLS in my response) but I'm not sure what you're asking about static methods.
Please re-read our responses and, if you have more questions, try to use an example or two to help us understand your trouble.
Abbu Rahman
Ranch Hand

Joined: Jan 05, 2004
Posts: 46
Thanks Mr.Corey McGlone,
i refresh the page, i get the answer ...cool
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is this OK!