Java Class New Instance newInstance(Field field)

Here you can find the source of newInstance(Field field)

Description

Get type of field and create an instance to that type

License

Apache License

Parameter

Parameter Description
field field to get type

Return

an instance of field's type

Declaration

public static Object newInstance(Field field) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    /**/*from  ww  w.  j a v a2  s  .  co  m*/
     * Get type of field and create an instance to that type
     * 
     * @param field field to get type
     * @return an instance of field's type
     */
    public static Object newInstance(Field field) {
        try {
            return field.getType().newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Can not new an instance of class " + field.getType() + " for field "
                    + field.getName() + " on class " + field.getDeclaringClass(), e);
        }
    }
}

Related

  1. newInstance(Class type, Object... params)
  2. newInstance(Constructor c, List parameters)
  3. newInstance(Constructor constructor, Object... args)
  4. newInstance(Constructor constructor, Object... arguments)
  5. newInstance(Constructor ctor, Object... params)
  6. newInstance(final Class clazz)
  7. newInstance(final Class clazz, final Object[] parameters)
  8. newInstance(final Class arrayClass, final int length)
  9. newInstance(final Class clazz)

  10. HOME | Copyright © www.java2s.com 2016