aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How String Can Be A Reference Variable... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How String Can Be A Reference Variable..." Watch "How String Can Be A Reference Variable..." New topic
Author

How String Can Be A Reference Variable...

Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61


Why This Code Is Not Showing Error...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

I assume your question is really "Why can we use the names of classes as names of variables?"

The answer to that is, the Java Language Specification doesn't forbid it. And really it can't, because people can create classes with any names they liked. Imagine if just creating a class named "Unit" caused some code you wrote two years ago to fail to compile because you named a variable "Unit".
Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
I cant Get You...Please Explain Me Very Clearly....How The Code Works....
Geraldo Maciel
Greenhorn

Joined: Oct 17, 2008
Posts: 2
Ok let me try.

The code doesn't show any erros because String is not an invalid identifier.

You can use the name of a class as an identifier with no problems.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

first of all you cannot use keywords as identifier names. String is not a keyword.

the compiler is not showing any error because it can differentiate between the two String.

String String = "Hello";

now if you write

String s = "garbage";

then the compiler knows that this String is java.lang.String.

and if you use

String = "garbage"

then the compiler knows that this String is the java.lang.String class reference variable created earlier as you cannot assign a value to a class...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
oh...ok...
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How String Can Be A Reference Variable...
 
Similar Threads
Java String pool
blackberry pearl 8100
Generic for HashMap holding different values
retreiving the session attribute value from anoher Action class
how to sovle these two errors~~~help~~~