Constructor

                                               
    java.lang.Object                                          
     |                                         
     |--java.lang.reflect.AccessibleObject                                      
         |                                     
         |--java.lang.reflect.Constructor                                  
                                               

Constructor provides information about a single constructor for a class.

Get the annotation declared for this constructor

ReturnMethodSummary
<T extends Annotation>T getAnnotation(Class<T> annotationClass) Returns this element's annotation for the specified type if such an annotation is present, else null.
Annotation[]getDeclaredAnnotations()Returns all annotations that are directly present on this element.

Get the declaring class

ReturnMethodSummary
Class<T>getDeclaringClass()Returns the Class object representing the class that declares the constructor represented by this Constructor object.

Get the generic exception types

ReturnMethodSummary
Class<?>[] getExceptionTypes()Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying constructor represented by this Constructor object.
Type[]getGenericExceptionTypes()Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.

Get the generic parameter types

ReturnMethodSummary
Type[]getGenericParameterTypes()Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.

Get the modifiers

ReturnMethodSummary
intgetModifiers()Returns the Java language modifiers for the constructor represented by this Constructor object, as an integer.

Get the name of the contructor

ReturnMethodSummary
StringgetName()Returns the name of this constructor, as a string.

Get parameter's annotation, type

ReturnMethodSummary
Annotation[][]getParameterAnnotations()Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Constructor object.
Class<?>[] getParameterTypes()Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Constructor object.

Get typed parameters

ReturnMethodSummary
TypeVariable<Constructor<T>>[] getTypeParameters()Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

Is this contructor a Synthetic, and does it have the variable number of arguments

ReturnMethodSummary
booleanisSynthetic()Returns true if this constructor is a synthetic constructor; returns false otherwise.
booleanisVarArgs()Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise.

Create new instance using the Contructor

ReturnMethodSummary
TnewInstance(Object... initargs)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.

String representation of the contructor

ReturnMethodSummary
StringtoGenericString()Returns a string describing this Constructor, including type parameters.
StringtoString()Returns a string describing this Constructor.
Revised from Open JDK source code

  import java.lang.reflect.Constructor;

public class Main {
  public static void main(String[] argv) throws Exception {
      Constructor[] constructors = String.class.getDeclaredConstructors();

      for (int i = 0; i < constructors.length; i++) {
        Constructor c = constructors[i];
        System.out.println(c.toGenericString());
      }
    }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.