Invoking non parameterized and parameterized constructors in java is possible thru reflection API. This helps the programmers to instantiate classes which are configured in a property file or in database on the runtime. Below given example demonstrate the power of jave reflection API, program creates instances using parameterized and non parameterized constructors.
Code snippet to invoke constructor using reflection
1 2 |
final Constructor<?> c = Class.forName("com.ourownjava.Person")getConstructor(); final Person person = (Person) c.newInstance(); |
Complete Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.ourownjava.corejava.reflection; import static org.junit.Assert.*; import static org.junit.Assert.assertNull; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import org.junit.Test; /** * * @author ourownjava.com * */ public class ReflectionTest { @Test public void shouldInvokeNonParamConstructor() throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { final Constructor<?> constructor = Class.forName( "com.ourownjava.corejava.reflection.Person").getConstructor(); final Person person = (Person) constructor.newInstance(); assertNotNull(person); assertNull(person.getName()); assertNull(person.getSocialSecurityNumber()); } @Test public void shouldInvokeStringParamConstructor() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { final Constructor<?> constructor = Class.forName( "com.ourownjava.corejava.reflection.Person").getConstructor( String.class); final Person person = (Person) constructor .newInstance("ourownjava.com"); assertNotNull(person); assertNotNull(person.getName()); assertNull(person.getSocialSecurityNumber()); assertEquals("ourownjava.com", person.getName()); } @Test public void shouldInvokeLongParamConstructor() throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { final Constructor<?> constructor = Class.forName( "com.ourownjava.corejava.reflection.Person").getConstructor( Long.class); final Person person = (Person) constructor.newInstance(90238454L); assertNotNull(person); assertNull(person.getName()); assertNotNull(person.getSocialSecurityNumber()); assertEquals(90238454L, person.getSocialSecurityNumber().longValue()); } @Test public void shouldInvokeTwoParamConstructor() throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { final Constructor<?> constructor = Class.forName( "com.ourownjava.corejava.reflection.Person").getConstructor( Long.class, String.class); final Person person = (Person) constructor.newInstance(90238454L, "ourownjava.com"); assertNotNull(person); assertNotNull(person.getName()); assertNotNull(person.getSocialSecurityNumber()); assertEquals(90238454L, person.getSocialSecurityNumber().longValue()); assertEquals(90238454L, person.getSocialSecurityNumber().longValue()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.ourownjava.corejava.reflection; /** * * @author ourownjava.com * */ public class Person{ String name; Long socialSecurityNumber; public Person(){ System.out.println("Person.Person()"); } public Person(String name){ System.out.println("Person.Person(String)"); this.name = name; } public Person(Long socialSecurityNumber){ System.out.println("Person.Person(Long)"); this.socialSecurityNumber = socialSecurityNumber; } public Person(Long empId, String name){ System.out.println("Person.Person(Long, String)"); this.socialSecurityNumber = empId; this.name = name; } public String getName() { return name; } public Long getSocialSecurityNumber() { return socialSecurityNumber; } } |