HOME       >>       Programming

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:

String s = "System";Class c = (Class)s; //not sure if this worksMethod 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.

leiaah

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:

Box b = new Box();  // ClassName a = new ClassName();
You can then access any variable in the Box class by doing this:

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:

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.

beeseven

The point is that I need an object of the class "Class" (http://forums.xisto.com/no_longer_exists/). 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.


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:

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.

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.


kvkv

Class c = (Class)s;

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

Class c = s.getClass();

That works :-)

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.


kvkv

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.

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.


VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.