Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Monday, February 16, 2009

Program to List constructors with name and signatures of class

import java.lang.reflect.Constructor;

/**

* List all the constructors with name and signatures present in the specified

* class. Make separate class and method for this functionality.

*

*

*/

public class ClassPropertyTest {

/**

* execution begins from here

*

* @param args

* commandline arguments

*/

public static void main(String[] args) {

// get the data of MyClass

new ClassPropertyTest().getMyClassData();

}

/**

* fetches all the constructor data of MyClass class

*

*/

public void getMyClassData() {

System.out.println("MyClass Details : ");

System.out.println("-----------------");

MyClass mcObj = new MyClass();

System.out.println("\tAvailable Constructors Are ::");

System.out.println("\t-----------------------------\n");

int i = 0;

for (Constructor cObj : mcObj.getClass().getDeclaredConstructors()) {

System.out.println("\tConstructor (" + (i++) + ") : " + cObj);

}

}

}

/**

* source class for fetching its constructor

*

*

*/

class MyClass {

// int member variable

private int intVar;

// default constructor

MyClass() {

intVar = 0;

}

// first paraemeterized constructor

MyClass(int intV) {

intVar = intV;

}

}

No comments:

Post a Comment

Followers