• Post Reply Bookmark Topic Watch Topic
  • New Topic

fully qualified name of nested class  RSS feed

 
Richard Hayward
Ranch Hand
Posts: 176
11
  • 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?


 
Roel De Nijs
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: 176
11
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!