Here you can find the source of newInstance(String className, Object[] args)
public static Object newInstance(String className, Object[] args)
//package com.java2s; /**// w ww. j ava2s .c o m * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); */ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Main { public static Object newInstance(String className, Object[] args) { try { Class newoneClass = Class.forName(className); Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } Constructor cons = newoneClass.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } public static Object newInstance(Class clazz, Object[] args) { try { Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } Constructor cons = clazz.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } public static Object newInstance(Class clazz, Object[] args, Class[] argsClass) { try { Constructor cons = clazz.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } public static Object newInstance(Class clazz) { try { Constructor cons = clazz.getConstructor(); return cons.newInstance(); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } public static Object newInstance(String className) { try { Class newoneClass = Class.forName(className); Constructor cons = newoneClass.getConstructor(); return cons.newInstance(); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } public static RuntimeException convertReflectionExceptionToUnchecked(Exception e) { if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException || e instanceof NoSuchMethodException) { return new IllegalArgumentException(e); } else if (e instanceof InvocationTargetException) { return new RuntimeException(((InvocationTargetException) e).getTargetException()); } else if (e instanceof RuntimeException) { return (RuntimeException) e; } return new RuntimeException("Unexpected Checked Exception.", e); } }