getDeclaredConstructors()
returns all the constructors declared by the class
represented by this Class object.
class Employee { String firstName, lastName; int id, age; Employee(String firstName){ this.firstName = firstName; } public Employee(String firstName, String lastName){ this.firstName = firstName; this.lastName = lastName; } public Employee(){ } Employee(String firstName, String lastName, int id){ this.firstName = firstName; this.lastName = lastName; this.id = id; } Employee(String firstName, String lastName, int id, int age){ this.firstName = firstName; this.lastName = lastName; this.id = id; this.age = age; } }
import java.lang.reflect.Constructor; public class GetConstructors { public static void main(String args[]){ Constructor[] con = Employee.class.getDeclaredConstructors(); System.out.println("List Of constructors available are"); for(Constructor c : con){ System.out.println(c); } } }
Output
List Of constructors available are Employee(java.lang.String,java.lang.String,int,int) Employee(java.lang.String,java.lang.String,int) public Employee() public Employee(java.lang.String,java.lang.String) Employee(java.lang.String)
There is
another method 'getConstructors()' which return only public
constructors.
import java.lang.reflect.Constructor; public class GetConstructors { public static void main(String args[]){ Constructor[] con = Employee.class.getConstructors(); System.out.println("List Of constructors available are"); for(Constructor c : con){ System.out.println(c); } } }
Output
List Of constructors available are public Employee() public Employee(java.lang.String,java.lang.String)
No comments:
Post a Comment