Example usage for java.lang Class newInstance

List of usage examples for java.lang Class newInstance

Introduction

In this page you can find the example usage for java.lang Class newInstance.

Prototype

@CallerSensitive
@Deprecated(since = "9")
public T newInstance() throws InstantiationException, IllegalAccessException 

Source Link

Document

Creates a new instance of the class represented by this Class object.

Usage

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getChar(x));

    f.setChar(x, 'a');
    System.out.println(f.getBoolean(x));

}

From source file:X.java

public static void main(String[] args) {
    try {//from ww  w  .jav  a  2s.  c  o m
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Type[] exp = method.getGenericExceptionTypes();
        for (Type anno : exp) {
            System.out.println(anno);
        }
        System.out.println();

    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:X.java

public static void main(String[] args) {
    try {//from w ww.  j a v a2  s .c  o  m
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Class<?>[] exp = method.getExceptionTypes();
        for (Class anno : exp) {
            System.out.println(anno);
        }
        System.out.println();

    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:X.java

public static void main(String[] args) {
    try {// w  w  w  .  jav  a  2  s. c o  m
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Annotation[] annos = method.getAnnotations();
        for (Annotation anno : annos) {
            System.out.println(anno);
        }
        System.out.println();

    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:MyClass.java

public static void main(String[] args) throws InstantiationException {
    Class<MyClass> personClass = MyClass.class;
    try {/* w w w  .j  av a  2  s.c  o  m*/
        MyClass p = personClass.newInstance();
        System.out.println(p);
    } catch (InstantiationException | IllegalAccessException e) {
        System.out.println(e.getMessage());
    }
}

From source file:Cls.java

public static void main(String... args) {
    try {/* ww  w  .  ja  va  2s .c o m*/
        Class<?> c = Class.forName("Cls");
        c.newInstance(); // InstantiationException

        // production code should handle these exceptions more gracefully
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> myClass = MyClass.class;
    try {//w  w  w  .  java  2  s .  c o m
        MyClass p = myClass.newInstance();
        Method setName = myClass.getMethod("setName", String.class);
        setName.invoke(p, "abc");
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException
            | IllegalArgumentException | InvocationTargetException e) {
        System.out.println(e.getMessage());
    }
}

From source file:MyClass.java

public static void main(String[] argv) throws Exception {

    URL[] urls = null;//from  ww  w.ja  v a  2s  .com

    File dir = new File(System.getProperty("user.dir") + File.separator + "dir" + File.separator);
    URL url = dir.toURI().toURL();
    urls = new URL[] { url };

    ClassLoader cl = new URLClassLoader(urls);

    Class cls = cl.loadClass("MyClass");

    MyClass myObj = (MyClass) cls.newInstance();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class<?> class1 = Class.forName("Rate");
    Object obj = class1.newInstance();
    for (Method m : class1.getMethods()) {
        if (m.getName().equals("myMethod")) {
            Class<?>[] parameterTypes = m.getParameterTypes();
            System.out.println(Arrays.toString(m.getParameterTypes()));
            Object methodArgs[] = new Object[parameterTypes.length];
            for (Class<?> parameterType : parameterTypes) {
                if (parameterType == Double.TYPE) {
                    double value = 0.5;
                    methodArgs[0] = value;
                }/*from  ww  w.ja  v  a 2 s .c  o  m*/
            }
            Rate rate = (Rate) m.invoke(obj, methodArgs);
            System.out.println(rate.getValue());
        }
    }
}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> my = MyClass.class;
    try {//w  w  w . j  a v  a2 s  .c om
        MyClass p = my.newInstance();
        Field nameField = my.getDeclaredField("name");
        nameField.setAccessible(true);
        String nameValue = (String) nameField.get(p);
        System.out.println("Current name is " + nameValue);
        nameField.set(p, "abc");
        nameValue = (String) nameField.get(p);
        System.out.println("New name is " + nameValue);
    } catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
            | IllegalArgumentException e) {
        System.out.println(e.getMessage());
    }
}