Java Class New Instance newInstance()

Here you can find the source of newInstance()

Description

new Instance

License

Apache License

Declaration

public static Object newInstance() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static Object newInstance() {
        try {/* ww w. j  a  va 2  s .  c o m*/
            return getTraceClass(1).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Class getTraceClass(int i) {
        StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
        StackTraceElement ele = stackTrace[1 + i];

        try {
            String className = ele.getClassName();
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }

    }
}

Related

  1. newInstance(Class clazz)
  2. newInstance(Class clazz)
  3. newInstance(Class clazz)
  4. newInstance(Class clazz)