getting a class if i know the class name

M

Marc E

I have a String whose value is "float". I need to turn that into float.class
for passing into a method (I need to do this for a number of
primitives....int, boolean, float, double, etc). Is there a built in way to
do this? Nothing jumped out at me in the Class class

thanks.
 
C

Chris

Marc E said:
I have a String whose value is "float". I need to turn that into
float.class for passing into a method (I need to do this for a number of
primitives....int, boolean, float, double, etc). Is there a built in way
to do this? Nothing jumped out at me in the Class class

First, think hard about why you're doing this. Manipulation of Class objects
is usually a bad idea if there is some other way to handle the problem.

That having been said, this may do the trick:

Class floatClass = Class.forName("java.lang.Float");
 
M

Marc E

I was doing this as a small experiment with reflection and calling
method.invoke. the arguments to the methods are primitive floats, ints, and
booleans, and so I had to pass float.class as the Class [] params to find
the method thorugh reflection. I tried forName("java.lang.float") but of
course got an error. I'll give yours a shot.

thanks.
 
C

Chris Smith

Marc E said:
I was doing this as a small experiment with reflection and calling
method.invoke. the arguments to the methods are primitive floats, ints, and
booleans, and so I had to pass float.class as the Class [] params to find
the method thorugh reflection. I tried forName("java.lang.float") but of
course got an error. I'll give yours a shot.

There's a difference between the Class object for Float and the Class
object for the primitive float. The code that Chris posted won't do the
same thing as what you've written.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
N

NullBock

So am I understanding you correctly? You want to use reflection for
calling a method with a primitive argument? This isn't a problem. Use
the TYPE field in the wrapper class associated with the primitive:

System.out.println(Float.TYPE);

output: float

Thus, if you wanted to call the Date(long) ctor, you would do something
like this:

Constructor ctor = Date.class.getConstructor(new Class[] {
Long.TYPE
});
Object[] args = {
new Long(0)
};
Date d = (Date) ctor.newInstance(args);

Is this what you want?

Walter Gildersleeve
Freiburg, Germany

___________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green
 
R

Roedy Green

I have a String whose value is "float". I need to turn that into float.class
for passing into a method (I need to do this for a number of
primitives....int, boolean, float, double, etc). Is there a built in way to
do this? Nothing jumped out at me in the Class class

I think you need a little HashMap to convert String to class literal.

You could see what pops out with Class.forName.

See http://mindprod.com/jgloss/classforname.html
 
R

Roedy Green

Class floatClass = Class.forName("java.lang.Float");

that is the Float class. Float.class is not the same as float.class.
float.class represents the primitive in a kludgy sort of way.
 
O

Oliver Wong

Marc E said:
I have a String whose value is "float". I need to turn that into
float.class for passing into a method (I need to do this for a number of
primitives....int, boolean, float, double, etc). Is there a built in way
to do this? Nothing jumped out at me in the Class class

Class.forName();

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#forName(java.lang.String)

Not sure if it works for primitives, but you can probably get
Float.class instead of float.class.

- Oliver
 
T

Thomas Hawtin

Oliver said:
Class.forName();

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#forName(java.lang.String)

Not sure if it works for primitives, but you can probably get
Float.class instead of float.class.

No it doesn't work. Class.forName("float") will attempt to find the
class float, strangely enough. Using a map is probably the best bet.

In any case, it is usually more appropriate to write something along the
lines of:

Class clazz = Class.forName(
className, true, Thread.currentThread().getContextClassLoader()
);

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top