Constructor toString() method in Java with Examples

The toString() method of java.lang.reflect.Constructor class is used to return string representation of this Constructor.This string is the collection of all the properties of this constructor. For creating this string method adds access modifiers of the constructor with the name of the declaring class of constructor then add parenthesized and add a comma-separated list of the constructor’s formal parameter types together.

Syntax:

public String toString()

Parameters: This method accepts nothing.



Return: This method returns string describing this Constructor.

Below programs illustrate toString() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate toString() method
  
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = shape.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        // check get string representation
        String str = cons[0].toString();
  
        // print result
        System.out.println("Constructor : " + str);
    }
  
    public class shape {
  
        public shape(Object... objects)
        {
        }
    }
}

chevron_right


Output:

Constructor : public GFG$shape(GFG, java.lang.Object[])

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate toString() method
  
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = String.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        for (int i = 0; i < cons.length; i++) {
  
            String str = cons[i].toString();
  
            // print result
            System.out.println(str);
        }
    }
}

chevron_right


Output:

public java.lang.String(byte[], int, int)
public java.lang.String(byte[], java.nio.charset.Charset)
public java.lang.String(byte[], java.lang.String) throws java.io.UnsupportedEncodingException
public java.lang.String(byte[], int, int, java.nio.charset.Charset)
public java.lang.String(byte[], int, int, java.lang.String) throws java.io.UnsupportedEncodingException
public java.lang.String(java.lang.StringBuilder)
public java.lang.String(java.lang.StringBuffer)
public java.lang.String(byte[])
public java.lang.String(int[], int, int)
public java.lang.String()
public java.lang.String(char[])
public java.lang.String(java.lang.String)
public java.lang.String(char[], int, int)
public java.lang.String(byte[], int)
public java.lang.String(byte[], int, int, int)

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#toString()




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.