Java Multiple Constructors Sample

Can you have multiple constructors, also known as constructor overloading, for the same class in the Java programming language? This is a common question that comes up in Java programming. YES you can! One of the key features of Java is called method overloading.

A constructor for a class in Java is nothing more than a method. Having multiple constructors or overloading the constructor is perfectly fine to do in Java. Just like any other method your overload, the constructor signatures must be different. Each constructor must have different data types as input.

Let’s take a look at the Java sample code below. We have actually created 9 different constructors for our sample class. This is a completely okay because each constructor input parameters is different and that is what method overloading is all about.

// Sample


public class JavaMultipleConstructors {

	public static void main(String[] args) {
		int myIntA = 5;
		int myIntB = 10;
		String myStr = "";
		char myChar = 'c';
		short myShort = 10;
		boolean myBool = true;
		long myLong = 12003456789L;
		double myDouble= 1.123456789;
		JavaMultipleConstructors jmc = new JavaMultipleConstructors();
		JavaMultipleConstructors jmcA = new JavaMultipleConstructors(myIntA);
		JavaMultipleConstructors jmcB = new JavaMultipleConstructors(myIntA, myIntB);
		JavaMultipleConstructors jmcC = new JavaMultipleConstructors(myStr);
		JavaMultipleConstructors jmcD = new JavaMultipleConstructors(myChar);
		JavaMultipleConstructors jmcE = new JavaMultipleConstructors(myShort);
		JavaMultipleConstructors jmcF = new JavaMultipleConstructors(myBool);
		JavaMultipleConstructors jmcG = new JavaMultipleConstructors(myLong);
		JavaMultipleConstructors jmcH = new JavaMultipleConstructors(myDouble);
	//Empty Constructor
	public JavaMultipleConstructors() {  
		System.out.println("Empty Constructor"); 
	//Constructor with single int
	public JavaMultipleConstructors(int takesInt) { 
		System.out.println("Single integer"); 
	//Constructor with two integers
	public JavaMultipleConstructors(int one, int two) { 
		System.out.println("Two integers"); 
	//Constructor with single String
	public JavaMultipleConstructors(String takesString) { 
		System.out.println("Single String"); 
	//Constructor with single character
	public JavaMultipleConstructors(char takesChar) { 
		System.out.println("Single Character"); 
	//Constructor with single short
	public JavaMultipleConstructors(short takesShort) { 
		System.out.println("Single Short"); 
	//Constructor with single byte
	public JavaMultipleConstructors(byte takesByte) { 
		System.out.println("Single Byte"); 
	//Constructor with single Boolean
	public JavaMultipleConstructors(Boolean takesBool) { 
		System.out.println("Single Boolean"); 
	//Constructor with single Long
	public JavaMultipleConstructors(Long takesLong) { 
		System.out.println("Single Long"); 
	//Constructor with single Double
	public JavaMultipleConstructors(Double takesDouble) { 
		System.out.println("Single Double"); 



Empty Constructor
Single integer
Two integers
Single String
Single Character
Single Short
Single Boolean
Single Long
Single Double

Add a Comment

Your email address will not be published. Required fields are marked *