• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting....

Bet this can be used as a trick question in the exam.
 
reply
    Bookmark Topic Watch Topic
  • New Topic