Nov 21, 2009

Can I Cast A String As A Class Object? - Using java.lang.Class

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

Can I Cast A String As A Class Object? - Using java.lang.Class

beeseven
As you may or may not know, there's a java.lang.Class class which handles stuff that's pretty cool. You can use it to get the methods of a class, constructors of a class, etc. Anyway, I'm kind of interested in making a simple Java interpreter, and if I were able to cast a String that contains the name of a class into a Class object, it would make that project infinitely easier. The most obvious thing would be to use a Class object constructor, but there aren't any. So basically, I want to do something like this:
CODE
String s = "System";
Class c = (Class)s; //not sure if this works
Method m = c.getMethod("currentTimeMillis", new Class[0]);
long n = m.invoke(null, new Object[0]);
//etc.

I'm not sure if the line (that has been clearly labeled) works, though. If anyone knows if this does or doesn't work and can suggest an alternative if it doesn't, the help would be greatly appreciated.

Comment/Reply (w/o sign-up)

leiaah
CODE
Class c = (Class)s;

I'm not sure about this code. I'm not sure if you can "typecast" a String variable to a Class variable but if you're going to instantiate or make a copy of a class you can do this:

CODE
Box b = new Box();  // ClassName a = new ClassName();

You can then access any variable in the Box class by doing this:

CODE
b.var_name;
b.meth_name(constructor);

You can only do this if the variable (or method) you're trying to access is declared publicly like this:

CODE
public String var_name="Box";

public void meth_name(){
      //code goes here...
}


Is this what you need? What I know about typecasting is that it will work only for related data types like Float to Integer or Double to Float.

 

 

 


Comment/Reply (w/o sign-up)

beeseven
The point is that I need an object of the class "Class" (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html). I can't use a constructor as you suggested because there aren't any. I just want to be able to take a String and use the class that's name is contained in the String- I won't know what it is, so it has to be able to work without knowing that. There'll be an input, then the program will use whatever class was inputted, but I can't just use an if/else if statement for three thousand classes.

Comment/Reply (w/o sign-up)

beeseven
I've thought of something else, I doubt it works but I'll test it. I'll put it here in case anybody else is working on this and maybe it will help him or her.

Since the getClass method is in Object, could I possibly cast an Object to another class and then use getClass on that? Casting is usually done using just parentheses and the class name, but can you use parentheses and a String? Something like:
CODE
Object x = new Object();
x = ("String")x;
So my question is, would that also work as a cast or just give some weird error/do nothing. As I said before, I'll test it, but maybe it helped someone.

Comment/Reply (w/o sign-up)

beeseven
Sorry about triple posting, but I think the search is over. After extensive research, I have found the answer to my problem in an official Sun Java tutorial. It uses the java.lang.reflect package and a couple other things. Here are some links related to it in case anybody wants them:

The reflection (java.lang.reflect) API
Using no-argument constructors for unknown classes
Using constructors with arguments for unknown classes
Getting a Class object knowing the package and name of the class you want

I guess this topic can be closed now.

Comment/Reply (w/o sign-up)

kvkv
CODE
Class c = (Class)s;


I don't know if the above works. But you can always use

CODE
Class c = s.getClass();


That works :-)

Comment/Reply (w/o sign-up)

beeseven
If you had read the topic, you would've known that I found my answer. Also, s.getClass() would return a String Class object, not whatever was in the string.

Comment/Reply (w/o sign-up)

kvkv
QUOTE(beeseven @ Feb 11 2006, 05:00 PM) *

If you had read the topic, you would've known that I found my answer. Also, s.getClass() would return a String Class object, not whatever was in the string.



Ok, Here you go. You need to use reflection to get the class definition of a class whose name is in a string. Once you get the class, you can even instantiate it.

CODE

import java.lang.reflect.*;
import java.awt.*;
......
//Your class in string
String s="<your class>";

//Get class definition
Class cls = Class.forName(s);

//Instantiate
Object obj=cls.newInstance();


There might be some exceptions which you need to catch (atleast provide empty catch blocks), but it works.


Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Cast Javalangclass


    Looking for cast, string, class, object, java, lang, class

Searching Video's for cast, string, class, object, java, lang, class
See Also,
advertisement


Can I Cast A String As A Class Object? - Using java.lang.Class

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com