reflection
1 import java.lang.reflect.Constructor;
2 public class TestList {
3 public static void main(String args[]) throws Exception {
4 Class c = Class.forName("com.**.MyClass");
5 //getDeclaredConstructors() reflecting all the constructors declared by the class
6 //getConstructors() just reflect the public constructors
7 Constructor constructors[] = c.getDeclaredConstructors();
8 Object obj = null;
9 for (Constructor cons : constructors) {
10 Class[] params = cons.getParameterTypes();
11 if (params.length == 1 && params[0] == int.class) {
12 obj = cons.newInstance(10);
13 break;
14 }
15
16 }
17 if (obj == null) {
18 System.out.println("Can’t Create MyClass object.");
19 return;
20 }
21 }
22 }
23
24
25
26 public class MyClass {
27 private int count;
28 MyClass(int c) {
29 System.out.println("MyClass(int):" + c);
30 count = c;
31 }
32 MyClass() {
33 System.out.println("MyClass()");
34 count = 0;
35 }
36 void setCount(int c) {
37 System.out.println("setCount(int): " + c);
38 count = c;
39 }
40 int getCount() {
41 System.out.println("getCount():" + count);
42 return count;
43 }
44 void showcount() {
45 System.out.println("count is " + count);
46 }
47 }
48
2 public class TestList {
3 public static void main(String args[]) throws Exception {
4 Class c = Class.forName("com.**.MyClass");
5 //getDeclaredConstructors() reflecting all the constructors declared by the class
6 //getConstructors() just reflect the public constructors
7 Constructor constructors[] = c.getDeclaredConstructors();
8 Object obj = null;
9 for (Constructor cons : constructors) {
10 Class[] params = cons.getParameterTypes();
11 if (params.length == 1 && params[0] == int.class) {
12 obj = cons.newInstance(10);
13 break;
14 }
15
16 }
17 if (obj == null) {
18 System.out.println("Can’t Create MyClass object.");
19 return;
20 }
21 }
22 }
23
24
25
26 public class MyClass {
27 private int count;
28 MyClass(int c) {
29 System.out.println("MyClass(int):" + c);
30 count = c;
31 }
32 MyClass() {
33 System.out.println("MyClass()");
34 count = 0;
35 }
36 void setCount(int c) {
37 System.out.println("setCount(int): " + c);
38 count = c;
39 }
40 int getCount() {
41 System.out.println("getCount():" + count);
42 return count;
43 }
44 void showcount() {
45 System.out.println("count is " + count);
46 }
47 }
48