Answers for "java get class"

1

get method of a class which I only have string to

String className = "my.class.name"; //Class name

Object classObj = null;
try {
  classObj = Class.forName(className);
} catch (ClassNotFoundException e) {
  e.printStackTrace();
}

Method classMethod = null;
try {
  classMethod = classObj.getClass().getMethod("method", param1.class, param2.class, ..);
} catch (SecurityException | NoSuchMethodException e) {
  e.printStackTrace();
}

try {
  classMethod.invoke(classObj, arg1, args2, ..);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException) {
  e.printStackTrace();
}
Posted by: Guest on February-24-2020
0

java get classname

//Just the name of the class
myObject.getClass().getSimpleName();

//The name of the class with the package name
myObject.getClass().getName();
Posted by: Guest on March-23-2021
1

java get class name of object

myObject.getClass().getName()
Posted by: Guest on December-26-2020
0

how to use getclass in java

public static String printClassInformation(Object o){
	Class<?> cl=o.getClass();
  	System.out.println("Name: "+cl.getSimpleName());
  	System.out.println("Full name: "+cl.getCanonicalName());
  	System.out.println("Methods: "+Arrays.toString(cl.getMethods()));
}
Posted by: Guest on April-06-2021
-1

java get class by string name

//get an according class variable
String className = "MyClass";
Class<?> cls = Class.forName(className);

//create instance of that class
Object myInstance = scl.newInstance();
Posted by: Guest on December-26-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language