• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How String Can Be A Reference Variable...

 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why This Code Is Not Showing Error...
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant Get You...Please Explain Me Very Clearly....How The Code Works....
 
Geraldo Maciel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh...ok...
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic