This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How String Can Be A Reference Variable...  RSS feed

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


Why This Code Is Not Showing Error...
 
Sheriff
Posts: 23714
50
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....
 
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.
 
Sheriff
Posts: 9613
37
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
Boost this thread!