Getting a class name without an instantiated object
Ron Aronica
Greenhorn
Joined: Mar 20, 2003
Posts: 14
posted
0
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
Joined: Jun 18, 2001
Posts: 64
posted
0
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
Joined: Jan 30, 2002
Posts: 3451
posted
0
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
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
James Swan
Ranch Hand
Joined: Jun 26, 2001
Posts: 403
posted
0
Or alternatively (if you're using 1.4.x):
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
posted
0
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
Joined: Nov 04, 2001
Posts: 1863
posted
0
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
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
Joined: Dec 12, 2002
Posts: 580
posted
0
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
Joined: Jun 18, 2001
Posts: 64
posted
0
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
Joined: Dec 12, 2002
Posts: 580
posted
0
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
Joined: Jan 04, 2003
Posts: 867
posted
0
hi Ron You can use getClass().getName() to show your class name if in the same file.
Francis Siu
SCJP, MCDBA
subject: Getting a class name without an instantiated object