Java Class New Instance newInstance(Class targetClass)

Here you can find the source of newInstance(Class targetClass)

Description

new Instance

License

Open Source License

Declaration

public static Object newInstance(Class targetClass)
            throws NoSuchMethodException, InvocationTargetException,
            IllegalAccessException, InstantiationException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /*from  w  ww.ja v  a 2 s .  c  o  m*/
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Main {
    private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
    private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];

    public static Object newInstance(Class targetClass)
            throws NoSuchMethodException, InvocationTargetException,
            IllegalAccessException, InstantiationException {
        // We want to get the default ctor, which is in the declared list. No need
        // to worry about inheritance because ctors are never virtual.
        Constructor ctor = targetClass
                .getDeclaredConstructor(EMPTY_CLASS_ARRAY);
        ctor.setAccessible(true);
        return ctor.newInstance(EMPTY_OBJECT_ARRAY);
    }
}

Related

  1. newInstance(Class cls)
  2. newInstance(Class cls)
  3. newInstance(Class cmdClass)
  4. newInstance(Class constructorClass, Object[] parameters)
  5. newInstance(Class klass)
  6. newInstance(Class cl, Object[] args)
  7. newInstance(Class clazz)
  8. newInstance(Class componentType, int length)
  9. newInstance(Class classZ)