Getting Class by Its Name

Getting class by its name

use forName instead..

something like this..

 try {
Class<?> act = Class.forName("com.bla.TestActivity");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

How to get a Class Object from the Class Name in Java

You can use:

Class c = Class.forName("com.package.MyClass");

And later instantiate an object:

Object obj = c.newInstance();

EDIT: This is just the simplest use case. As indicated in the comments, you will need to consider constructor arguments and exceptions thrown by the initialization process. The JavaDocs for newInstance has all the details.

How can I get a class by its name in Python

You can simply use getattr on the module :

import my_module

getattr(my_module, class_name)

Java - get the current class name?

The "$1" is not "useless non-sense". If your class is anonymous, a number is appended.

If you don't want the class itself, but its declaring class, then you can use getEnclosingClass(). For example:

Class<?> enclosingClass = getClass().getEnclosingClass();
if (enclosingClass != null) {
System.out.println(enclosingClass.getName());
} else {
System.out.println(getClass().getName());
}

You can move that in some static utility method.

But note that this is not the current class name. The anonymous class is different class than its enclosing class. The case is similar for inner classes.

Is there a way to instantiate a class by name in Java?

Two ways:

Method 1 - only for classes having a no-arg constructor

If your class has a no-arg constructor, you can get a Class object using Class.forName() and use the newInstance() method to create an instance (though beware that this method is often considered evil because it can defeat Java's checked exceptions).

For example:

Class<?> clazz = Class.forName("java.util.Date");
Object date = clazz.newInstance();

Method 2

An alternative safer approach which also works if the class doesn't have any no-arg constructors is to query your class object to get its Constructor object and call a newInstance() method on this object:

Class<?> clazz = Class.forName("com.foo.MyClass");
Constructor<?> constructor = clazz.getConstructor(String.class, Integer.class);
Object instance = constructor.newInstance("stringparam", 42);

Both methods are known as reflection. You will typically have to catch the various exceptions which can occur, including things like:

  • the JVM can't find or can't load your class
  • the class you're trying to instantiate doesn't have the right sort of constructors
  • the constructor itself threw an exception
  • the constructor you're trying to invoke isn't public
  • a security manager has been installed and is preventing reflection from occurring

Get members of class from class name

If the classes have already been loaded by the jvm, you can use the static method of Class called Class.forName(String className); and it will return you a handle to the reflections object.

you would do:

//get class reflections object method 1
Class aClassHandle = Class.forName("MyClass");

//get class reflections object method 2(preferred)
Class aClassHandle = MyClass.class;

//get a class reflections object method 3: from an instance of the class
MyClass aClassInstance = new MyClass(...);
Class aClassHandle = aClassInstance.getClass();

//get public class variables from classHandle
Field[] fields = aClassHandle.getFields();

//get all variables of a class whether they are public or not. (may throw security exception)
Field[] fields = aClassHandle.getDeclaredFields();

//get public class methods from classHandle
Method[] methods = aClassHandle.getMethods();

//get all methods of a class whether they are public or not. (may throw security exception)
Method[] methods = aClassHandle.getDeclaredMethods();

//get public class constructors from classHandle
Constructor[] constructors = aClassHandle.getConstructors();

//get all constructors of a class whether they are public or not. (may throw security exception)
Constructor[] constructors = aClassHandle.getDeclaredConstructors();

To get a variable named b from MyClass, one might do.

Class classHandle = Class.forName("MyClass");
Field b = classHandle.getDeclaredField("b");

and if b is type integer, to get its value i would do.

int bValue = (Integer)b.get(classInstance);//if its an instance variable`

or

int bValue = (Integer)b.get(null);//if its a static variable

getting only name of the class Class.getName()

Class.getSimpleName()

How to get class Object by using class Name at runtime in java

Maybe I don't understand your question, but if you are looking to create a class instance only using your class name, you can try this method:

public static MyClass getFromString(String myClassName) {
Object result = null;
MyClass final_result = null;

try{
result = Class.forName(myClassName).getConstructor().newInstance();
final_result= (MyClass)result;
}catch(ClassNotFoundException e){} //you must specify concrete Exception here

return final_result;
}

Hope it helps.



Related Topics



Leave a reply



Submit