Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Constructor in java - Testingpool

Constructor in java

Constructor is an interesting concept in java. A constructor is used to initialize the object.

In other words, a constructor is invoked when we create any object.A constructor initializes the data for the object.

Rules for constructor:

  1. Constructor name must be same as the class name
  2. Constructor must not have any explicit return type

There are 2 types of constructors in java.

  1. Default constructor
  2. Parameterized constructor

Default Constructor: A constructor that does not have any arguments is called default constructor.

Note: If no constructor is provided, compiler adds a default constructor to the class.

Constructor in java

When we create the object of SubNumber class in the below example, default constructor will be called.

package com.testingpool.demo2;

public class SubNumber {
		
	SubNumber(){
		System.out.println("This is a default constructor");
	}
	public static void main(String[] args) {		
		SubNumber sub = new SubNumber();
	}

}
Output : This is a default constructor

Parameterized Constructor: A constructor which has parameters is known as Parameterized constructor. We can have any number of parameters in the constructor and we can initialize objects with the same no. of parameters as per parameterized constructor.

package com.testingpool.demo2;

public class SubNumber {
		
	SubNumber(int a,int b){   // parameterized constructor
		int c = a+b;
		System.out.println("The sum is "+c);
	}
	public static void main(String[] args) {		
		SubNumber sub = new SubNumber(4,5);   //create object
	}

}
Output : The sum is 9

What is constructor overloading?

That means we can have more than one constructor but with different number of parameters.

package com.testingpool.demo2;

public class SubNumber {
		
	SubNumber(int a,int b){   // 1st parameterized constructor 1
		int c = a+b;
		System.out.println("Output of 1st constructor "+c);
	}
	
	SubNumber(int x,int y,int z){   // 1st parameterized constructor 1
		int d = x+y+z;
		System.out.println("Output of 2nd constructor "+d);
	}
	public static void main(String[] args) {		
		SubNumber sub = new SubNumber(4,5);   //create object for 1st constructor
		SubNumber sub1 = new SubNumber(2,4,6);   //create object for 1st constructor
	}

}
Output: Output of 1st constructor 9 Output of 2nd constructor 12

What is the role of a default constructor?

Default constructor assigns the default values to object or data members like 0 to int, null to String etc.

package com.testingpool.demo2;

public class SubNumber {
	
	int a;
	String b;
		
	SubNumber(){

		System.out.println(a +"----"+b);
	}

	public static void main(String[] args) {		
		SubNumber sub = new SubNumber();   //create object for constructor
	}

}
Output: 0—-null

Does constructor return any value?

Yes, it returns class instance. We cant use return type.

What else constructor can do?

It can be used like a method i.e. we can create object, call methods etc inside it.

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...