Create new Instance - Java Reflection

Java examples for Reflection:Invoke

Description

Create new Instance

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class cls = String.class;
        System.out.println(newInstance(cls));
    }/*from  ww w. j a  v  a  2  s  .  c  om*/

    public static Object newInstance(Class<?> cls) {
        Object obj = null;
        try {
            obj = cls.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }
}

Related Tutorials