Java Class New Instance newInstance(Class clazz)

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

Description

Returns a new instance of the specified class.

License

Open Source License

Parameter

Parameter Description
clazz The class to instantiate

Exception

Parameter Description
IllegalAccessException an exception
InstantiationException an exception

Declaration

public static Object newInstance(Class clazz) throws InstantiationException, IllegalAccessException 

Method Source Code

//package com.java2s;
/*/*w  ww  . j a v  a 2 s .  co m*/
 * Copyright (c) 2013-2015 Netcrest Technologies, LLC. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Returns a new instance of the specified class. If the specified class is
     * primitive then it returns the default value of that type. For String, it
     * returns an empty string. For enum, it returns the first enum value if
     * any. For all others, it instantiates the class by invoking the default
     * constructor.
     * 
     * @param clazz
     *            The class to instantiate
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static Object newInstance(Class clazz) throws InstantiationException, IllegalAccessException {
        if (clazz == String.class) {
            return "";
        } else if (clazz == boolean.class || clazz == Boolean.class) {
            return false;
        } else if (clazz == byte.class || clazz == Byte.class) {
            return (byte) 0;
        } else if (clazz == char.class || clazz == Character.class) {
            return (char) 0;
        } else if (clazz == short.class || clazz == Short.class) {
            return (short) 0;
        } else if (clazz == int.class || clazz == Integer.class) {
            return (int) 0;
        } else if (clazz == long.class || clazz == Long.class) {
            return (long) 0;
        } else if (clazz == float.class || clazz == Float.class) {
            return (float) 0;
        } else if (clazz == double.class || clazz == Double.class) {
            return (double) 0;
        } else {
            Object c[] = clazz.getEnumConstants();
            if (c == null) {
                return clazz.newInstance();
            } else if (c.length > 0) {
                return c[0];
            } else {
                return null;
            }
        }
    }
}

Related

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