Create an object of a class without new keyword.
The newInstance method of Class class can be used for creating an object of a class without using new keyword.
public class ClazzUtil {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class clazz = Class.forName("java.lang.Thread");
Thread obj = (Thread) clazz.newInstance();
System.out.println(obj);
}
}
Tags: Class, Class examples, java.lang, java.lang.Class
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply