Java Class New Instance newInstance(String className)

Here you can find the source of newInstance(String className)

Description

This method will create a new instance of the class specified by className.

License

Apache License

Parameter

Parameter Description
className a parameter

Declaration

public static Object newInstance(String className) 

Method Source Code

//package com.java2s;
/*//from w w w  .  j  a  va2  s. c  o  m
 * MODIFICATIONS to the original source have been made by
 * Scott Douglass <scott@swdouglass.com>
 *
 * Copyright 2009 Scott Douglass <scott@swdouglass.com>
 * 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.
 *
 * The original copyright notice and license terms are below.
 */

public class Main {
    /**
     * This method will create a new instance of the class specified by className.
     *
     * @param className
     * @return
     */
    public static Object newInstance(String className) {
        try {
            return Class.forName(className).newInstance();
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("Not found " + className);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("No access to " + className);
        } catch (InstantiationException e) {
            throw new IllegalArgumentException("Cannot instantiate " + className);
        }
    }
}

Related

  1. newInstance(final String fullyQualifiedClass)
  2. newInstance(Map> providerMap, String provider, Class[] paramsClass, Object[] paramValues)
  3. newInstance(Object bean, String propertyName, Class clazz)
  4. newInstance(ProceedingJoinPoint thisJoinPoint, Class clazz, Object... newArgs)
  5. newInstance(String aclass)
  6. newInstance(String className)
  7. newInstance(String className)
  8. newInstance(String className)
  9. newInstance(String className)