wood burning stoves 2.0*
The moose likes Java in General and the fly likes reflection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "reflection problem" Watch "reflection problem" New topic
Author

reflection problem

Tal Goldstein
Greenhorn

Joined: Apr 16, 2006
Posts: 14
hello
supose i have a String: "int".
i would like to get the class of what inside the string.
i can't use Class.forName() in this case because the string contains a type which is a primitive (int).
and of course if i'll do "int".getClass() i will get the class of java.lang.String which is not what i want.

any ideas?

thanks
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Tal,
"int" is not a class; it is a primitive. This means it is built in and there isn't a class that defines it. There is a logical Object equivalent - Integer.

If you want this, you could write a method to see if the String contains a primitive and map to the appropriate value yourself. There aren't too many of these so you could list them out in the code (or better yet in a Map.) If the type you are looking for is not in the list, you could then proceed to use Class.forName.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Actually, there is a Class for primitive "int". It is "Integer.TYPE". This is not the same as "Integer.class", which is the Class for Integer.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Tal Goldstein
Greenhorn

Joined: Apr 16, 2006
Posts: 14
Originally posted by Jeanne Boyarsky:
Tal,
"int" is not a class; it is a primitive. This means it is built in and there isn't a class that defines it. There is a logical Object equivalent - Integer.

If you want this, you could write a method to see if the String contains a primitive and map to the appropriate value yourself. There aren't too many of these so you could list them out in the code (or better yet in a Map.) If the type you are looking for is not in the list, you could then proceed to use Class.forName.


as someone already said, although int is a primitive type, there's still a class representing it. try: int.class
an you'll get that class.
Peter - thanks for the Integer.TYPE soulution
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Integer.TYPE can also be referenced as int.class. These two mean the same thing, a Class instance referring to the primitive type int. As opposed to Integer.class, which refers to the wrapper Integer.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reflection problem