Here you can find the source of newInstance(Class> cls)
private static Object newInstance(Class<?> cls)
//package com.java2s; //License from project: Apache License import java.lang.reflect.*; public class Main { private static Object newInstance(Class<?> cls) { try {/*from w w w . j a va 2 s . c om*/ return cls.newInstance(); } catch (Throwable t) { try { Constructor<?>[] constructors = cls.getConstructors(); if (constructors != null && constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName()); } Constructor<?> constructor = constructors[0]; if (constructor.getParameterTypes().length > 0) { for (Constructor<?> c : constructors) { if (c.getParameterTypes().length < constructor.getParameterTypes().length) { constructor = c; if (constructor.getParameterTypes().length == 0) { break; } } } } return constructor.newInstance(new Object[constructor.getParameterTypes().length]); } catch (InstantiationException e) { throw new RuntimeException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } } } }