How to Use Constructor Chaining in Java


This example is to show how to use Constructor chain in Java.

Source Code

package com.beginner.examples;

public class ConstructorChainExample {

	public ConstructorChainExample(){
        System.out.println("Default constructor");
    }
    public ConstructorChainExample(String str){
    	this();// call default constructor
        System.out.println("Constructor with String object");
    }
     
    public static void main(String a[]){
    	ConstructorChainExample c = new ConstructorChainExample("java");
    }
}

Output:

Default constructor
Constructor with String object
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments