tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > Commons Lang3 > Reflection > Get Constructor

Get Constructor 

Apache Commons Lang 3.0 is a java library with lot of utilities and reusable components. This requires the library commons-lang3-3.0.1.jar to be in classpath. The following example shows using ConstructorUtils.getAccessibleConstructor() API. It returns the accessible constructor given the class name and constructor signature. It returns null if no such constructor is available.

File Name  :  
com/bethecoder/tutorials/commons_lang/common/Student.java 
   
package com.bethecoder.tutorials.commons_lang.common;

public class Student extends Person {

  private static String DEFAULT_NAME = "Unknown";
  private String name;
  private int age;
  private String hobby;

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/commons_lang/common/Person.java 
   
package com.bethecoder.tutorials.commons_lang.common;

public class Person {

  private double height;
  private double weight;
  
  public double getHeight() {
    return height;
  }
  public void setHeight(double height) {
    this.height = height;
  }
  public double getWeight() {
    return weight;
  }
  public void setWeight(double weight) {
    this.weight = weight;
  }
}
   

File Name  :  
com/bethecoder/tutorials/commons_lang/tests/reflections/GetConstructorTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.commons_lang.tests.reflections;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.lang3.reflect.ConstructorUtils;

import com.bethecoder.tutorials.commons_lang.common.Student;

public class GetConstructorTest {

  /**
   @param args
   @throws InstantiationException 
   @throws InvocationTargetException 
   @throws IllegalAccessException 
   @throws NoSuchMethodException 
   */
  public static void main(String[] argsthrows NoSuchMethodException, 
      IllegalAccessException, InvocationTargetException, 
      InstantiationException {

    Constructor<Student> cons = 
      ConstructorUtils.getAccessibleConstructor(Student.class);
    System.out.println(cons);

    Constructor<Student> intCons = 
      ConstructorUtils.getAccessibleConstructor(Student.class, 
          String.class, int.class, String.class);
    System.out.println(intCons);

    Constructor<Student> noCons = 
      ConstructorUtils.getAccessibleConstructor(Student.class,
          Double.class, Float.class, Short.class);
    System.out.println(noCons);
  }

}
   

It gives the following output,
public com.bethecoder.tutorials.commons_lang.common.Student()
public com.bethecoder.tutorials.commons_lang.common.Student(java.lang.String,int,java.lang.String)
null


                    Print

   



Books Recommendation

 
 
bl  br