This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Fully Qualified Path Names (help!)

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to get the fully qualified path name of a class in the following code:
public class X{

//NOTE: This is a static variable!
public static final String fullyQualifiedPathName = ???;
}
I'd like to use the getName from the Class class method, however, I can't find a way to get a reference to the class of the object statically. Any suggestions??
Reference the Class class and the Object class.
Thanks,
Landon Manning
lmanning@symantec.com
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you create an instance of the class?
If so, then you can do:
X myX = new X();
Class classX = myX.getClass();
Stirng name = classX.getName();
Of course, as long as the variable is final, it needs to be
decalred in the static initializer, before you can get an instance. Can you not make it final, and set it immediatwely after you construct it?
I'll bet you can do this if you write your own classloader (since basically a static final field gets set as soon as the class is first loaded).

--Mark
hershey@eyeshake.com
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get a Class object by using the .class syntax. For example,
public static final String fullPath = X.class.getName();
will do what you need without requiring instantiation.
 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, thanks. Would you care to go into a little detail about what is going on here? (as I have never seen this syntax before)
Thanks
Landon Manning
Symantec
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmmm that does work, but where does the static class variable come from ... i would have thought it should be listed as a field in the javadoc entry for the Object class, but it isnt ...
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carl
I did the exact same thing as you did. No class as a field anywhere, not in Class class, not in Object class, absolutely no where. Then, I tried a simple code like this:
public class MyClass{
public static void main(String[] s) {
MyClass m = new MyClass();
System.out.println(MyClass.class);
System.out.println(m.getClass());
}
}
Then I realized what Jim linked from JLS. The class is not a field, it is just a Java keyword can be used this way.
Roseanne

[This message has been edited by Roseanne Zhang (edited December 05, 2000).]
 
Carl Desborough
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool ... thank you .. heres the text from the jls ammendment for 1.1:
D.7.3 Getting the Class Object for a Named Class
You can get the Class object for a given type with a new use of the class keyword:
Class threadClass = Thread.class;
Class intClass = int.class;
Class voidClass = void.class;
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic