Constructor getName() method in Java with Examples
The constructor class provides information about a single constructor for a class and it also provides access to that constructor.
The getName() method of java.lang.reflect.Constructor is used to return this constructor name, as a string. Constructor name is the binary name of the constructor’s declaring class.
Syntax:
public String getName()
Parameters: This method accepts nothing.
Return: This method returns the simple name of the underlying member in String format.
Below programs illustrate getName() method:
Program 1:
import java.lang.reflect.Constructor;
public class GFG {
public static void main(String[] args)
{
Class classObj = String. class ;
Constructor[] con
s
= classObj.getConstructors();
System.out.println( "Constructor : "
+ cons[ 0 ].getName());
}
}
|
Output:
Constructor : java.lang.String
Program 2:
import java.lang.reflect.Constructor;
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
Constructor constructor
= ArrayList. class .getConstructors()[ 0 ];
String name = constructor.getName();
System.out.println( "Constructor Name : "
+ name);
}
}
|
Output:
Constructor Name : java.util.ArrayList
References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getName(java.lang.Object)
Last Updated :
29 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...