• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

"Class names can serve as a valid identifier" Why?

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enthuware says, "Class names can serve as a valid identifier"

For instance:



is a valid statement?


Doesn't this introduce confusion and errors, perhaps even worse than shadowing? What is the logic of Java programming language to allow a Class name to serve as a valid identifier?
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can create a new class and call it, say, "Thumbnail". Does that mean that I shouldn't be able to name any of my variables "Thumbnail"? What would happen if I already had a project with several hundred classes and many of them had variables named "Thumbnail", and then the new intern came in and created a "Thumbnail" class? Should my old code not compile any more? That wouldn't be nice.

Yes, it can be confusing. And yes, it's bad form to do that. It's just that it's impossible to write a specification which defines what sequences of characters can serve as variable names if you have to impose a condition like that.
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to the confusion, you can create generic types of class names like MyClass<String> here the String is a new type which we created (like E or T) not java.lang.String (unrelated but I thought you should know)...
 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. This explains why the class names from the JDK library are not classed as being reserved words.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting....

Bet this can be used as a trick question in the exam.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!