CodesJava

Easy learning with example program codes

Java reflection get public constructor

The getConstructor() method is used to get the public constructor in java.

Example

TestClass.java

package com.codesjava;
 
public class TestClass {
	private String message;
 
	public TestClass(String message){
		this.message = message;
	}
 
	private void display(){
		System.out.println(message);
	}
}

ReflectionTest.java

package com.codesjava;
 
import java.lang.reflect.Constructor;
import java.util.Arrays;
 
public class ReflectionTest {
	public static void main(String args[]){
		try {
			Class c=Class.forName("com.codesjava.TestClass");  
			Constructor<?> constructor = c.getConstructor(String.class);
			System.out.println(Arrays.toString(constructor.getParameterTypes()));
		} catch (Exception e) {
			e.printStackTrace();
		}  
	}
}

Output

[class java.lang.String]
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status