This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

fully qualified name of nested class  RSS feed

 
Ranch Hand
Posts: 205
13
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code, I have a static nested class Dog.

output:

Within main of the outer class, I'm using Class.forName() to get a class literal and then using that class literal to create a Dog.
At line 12, the dog proves it really is a Dog, by barking.
What seems odd is that, the String I supply as argument to Class.forName() must be "ClassLiteralTest$Dog".
I know that on compilation a .class file with $ separated name is generated, but elsewhere in code, if I want to refer to or create a Dog, as at line 17, I'd use dot syntax. Similarly, Dog.class.getCanonicalName() returns a dot separated name.

So, is the way to understand this, that the fully qualified name of a class, as required by Class.forName(), is the name in the host os file system less '.class', which isn't always the same as the canonical name?


 
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard Hayward wrote:What seems odd is that, the String I supply as argument to Class.forName() must be "ClassLiteralTest$Dog".


That's not odd, that's just how it works More info can be found in this topic.
 
Richard Hayward
Ranch Hand
Posts: 205
13
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:More info can be found in this topic.


Thanks Roel, I'll study that thread.
 
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!