• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a class name without an instantiated object

 
Ron Aronica
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to set a static String class variable equal to the name of the class. I do not want to use a constant as the entire purpose for doing this is to allow the class to be renamed without having to change the variable's value. Can I use Class' methods to do this even though I do not have an instance of the class? If not, is there another way to accoumplish this short of setting the variable in the constructors?
 
Chris Treglio
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about everybody else, but I found this to be a really tough question... but I'm proud to say that I think I have the most backward way of solving it

of course, the exact string parsing depends on your runtime's style of stack traces...
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ron,
Welcome to JavaRanch. Umm, not totally clear on what you want here so I'll throw some code at you and see if I'm on the same page with you:

Now that's not groundbreaking code, for one thing the getObjectByName() method can only return an instance of a class that has a public default constructor. But it does demonstrate that with reflection, it is possible to create an instance if all you have is the name of the class. Also to do anything to our instantiated classes, we still will need to typecast them to the proper type which can't be done with a String, we need to know the class at compile time.
Michael Morris
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or alternatively (if you're using 1.4.x):
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I'm apparently not on the same page here. . Good hack Chris and James, I wouldn't have thought of trying that. It seems to show a deficiency in Reflection though: that one must resort to unconventional means to get the enclosing class from a static context.
Michael Morris
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ron,
sorry but i'm not able to understand the "need" of what you are trying to do.
why one would need to know the class name w/o creating any instance of it?
also, if you don't want to instantiate an object unnecessarily to know the class name then i would say the approach via throwing an exception doesn't buy us anything. does it? because we endup creating instance of the Throwable.
regards
maulin
 
Chris Treglio
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the record, I agree with Maulin. I just thought it was a challenge to get the classname from a static context, I'd never want to write code like I suggested above.
I'm guessing that Ron wants the String classname to be a member of his class so that elsewhere in his app he can refer to it. But Ron should really just use obj.getClass().getName() wherever else in the app he was planning on using obj.name. It can't always be used in a static context... can it?
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Youc an do this :

You will have to change the code when you rename the class but at least it wont compile so you wont forget.
 
Chris Treglio
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the old MyClass.class is still available to your compiler somewhere, that would compile and print "MyClass" just fine; class and getName() are both public.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well in that case you didn't properly rename the file. If you leave old unused class files on your classpath you are in for a World of hurt anyway.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ron
You can use getClass().getName() to show your class name if in the same file.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic