Getting Class type from String

JavaClassReflection

Java Problem Overview


I have a String which has a name of a class say "Ex" (no .class extension). I want to assign it to a Class variable, like this:

Class cls = (string).class

How can i do that?

Java Solutions


Solution 1 - Java

Class<?> cls = Class.forName(className);

But your className should be fully-qualified - i.e. com.mycompany.MyClass

Solution 2 - Java

String clsName = "Ex";  // use fully qualified name
Class cls = Class.forName(clsName);
Object clsInstance = (Object) cls.newInstance();

Check the Java Tutorial trail on Reflection at http://java.sun.com/docs/books/tutorial/reflect/TOC.html for further details.

Solution 3 - Java

You can use the forName method of Class:

Class cls = Class.forName(clsName);
Object obj = cls.newInstance();

Solution 4 - Java

You can get the Class reference of any class during run time through the Java Reflection Concept.

Check the Below Code. Explanation is given below

Here is one example that uses returned Class to create an instance of AClass:

package com.xyzws;
class AClass {
    public AClass() {
        System.out.println("AClass's Constructor"); 
    }  
    static {   
        System.out.println("static block in AClass");  
    }
}
public class Program {   
    public static void main(String[] args) {
        try {       
            System.out.println("The first time calls forName:");   
            Class c = Class.forName("com.xyzws.AClass");      
            AClass a = (AClass)c.newInstance();    
            System.out.println("The second time calls forName:");  
            Class c1 = Class.forName("com.xyzws.AClass"); 
        } catch (ClassNotFoundException e) { 
            // ...
        } catch (InstantiationException e) {  
            // ...
        } catch (IllegalAccessException e) { 
            // ...
        }     
    }
}

The printed output is

    The first time calls forName:
    static block in AClass
    AClass's Constructor
    The second time calls forName:
    

The class has already been loaded so there is no second "static block in AClass"

The Explanation is below

Class.ForName is called to get a Class Object

By Using the Class Object we are creating the new instance of the Class.

Any doubts about this let me know

Solution 5 - Java

It should be:

Class.forName(String classname)

Solution 6 - Java

Not sure what you are asking, but... Class.forname, maybe?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStevenView Question on Stackoverflow
Solution 1 - JavaBozhoView Answer on Stackoverflow
Solution 2 - JavaJuanZeView Answer on Stackoverflow
Solution 3 - JavarspView Answer on Stackoverflow
Solution 4 - JavagmhkView Answer on Stackoverflow
Solution 5 - JavaHans WesterbeekView Answer on Stackoverflow
Solution 6 - JavaManrico CorazziView Answer on Stackoverflow