Java Class New Instance newInstance(Class klass, Object... args)

Here you can find the source of newInstance(Class klass, Object... args)

Description

Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters.

License

Apache License

Parameter

Parameter Description
T type for the new instance
klass the Class object.
args array of objects to be passed as arguments to the constructor call.

Return

a new object created by calling the constructor this object represents.

Declaration

public static <T> T newInstance(Class<T> klass, Object... args) 

Method Source Code

//package com.java2s;
/**//from  www .  java 2s .  c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.lang.reflect.Constructor;

public class Main {
    /**
     * Uses the constructor represented by this {@code Constructor} object to
     * create and initialize a new instance of the constructor's declaring
     * class, with the specified initialization parameters.
     *
     * @param <T> type for the new instance
     * @param klass the Class object.
     * @param args array of objects to be passed as arguments to the constructor
     *        call.
     * @return a new object created by calling the constructor this object
     *         represents.
     */
    public static <T> T newInstance(Class<T> klass, Object... args) {
        try {
            Constructor<T> ctor;

            if (args.length == 0) {
                ctor = klass.getDeclaredConstructor();
            } else {
                Class<?>[] argClses = new Class[args.length];
                for (int i = 0; i < args.length; i++) {
                    argClses[i] = args[i].getClass();
                }
                ctor = klass.getDeclaredConstructor(argClses);
            }
            ctor.setAccessible(true);
            return ctor.newInstance(args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. newInstance(Class klass)
  2. newInstance(Class klass)
  3. newInstance(Class klass)
  4. newInstance(Class klass)
  5. newInstance(Class klass, Class[] paramTypes, Object... params)
  6. newInstance(Class listener)
  7. newInstance(Class sourceClass)
  8. newInstance(Class targetType, String className, ClassLoader loader)
  9. newInstance(Class tClass, Object... initargs)