What is Constructor in Java With Example

Constructor in Java: –

  1. A constructor should have the same name as the class.
  2. Every time we create an object constructor is called.

Example 1

public class A {
	A() {
		System.out.println(100);
	}
	public static void main( String[] args) {
	A a1 = new A();
	
}
}
Output : 100

Example 2

public class A {
	A(){
		System.out.println(100);
	}
	public static void main( String[] args) {
	A a1 = new A();
	A a2 = new A();
	A a3 = new A();
	
}
}
Output : 
100
100
100

Example 3

public class A {
	A(int x,float f,String s){
		System.out.println(x);
		System.out.println(f);
		System.out.println(s);
	}
	public static void main( String[] args) {
	A a1 = new A(10,10.5f,"raju");
	
	
}
}
Output: 
10
10.5
raju

3. Constructors are Permanently void, Which Means they Can Never return Value.

Example 1 Constructor in Java

public class A {
	A(){
		return 100;   // Error
	
	}
	public static void main( String[] args) {
	
}
}

Example 2

public class A {
	A(){
		return;        //Correct
	
	}
	public static void main( String[] args) {
	
}
}

4. Do not give a return Type to Constructor, if You do that it will be treated as Method.

Example 1

public class A {
	void A(){   // Method
		System.out.println(10);
	
	}
	public static void main( String[] args) {
	A a1 = new A();
	
}
}
Output:  

You Also Learn –

Example 2

public class A {
	void A(){  // Method
		System.out.println(10);
	
	}
	public static void main( String[] args) {
	A a1 = new A();
	a1.A;
}
}
Output: 10

What is Constructor Overloading With Example

Constructor Overloading: – Here We Can Create More Than One Constructor in the Same Class provided they have a Different Number Of Arguments or Different types of Arguments.

Example 1

public class A {
	 A(){  
		System.out.println(10);
	}
	A(int x){
		System.out.println(x);
	}
	A(float f){
		System.out.println(f);
	}
	public static void main( String[] args) {
		A a1 = new A();
		A a2 = new A(100);
		A a3 = new A(10.3f);

}
}
Output: 
10
100
10.3

What is Constructor Chaining in Java With Example?

Constructor Chaining in Java: – When You Call One Constructor From Another Constructor, it is Called Constructor Chaining.

Example 1

public class A {
	 A(){   
		System.out.println(10);
	}
	A(int x){
		this();
	}
	public static void main( String[] args) {
		A a1 = new A(100);
		
}
}
Output : 10

Example 2

public class A {
	 A(int x){   
		System.out.println(x);
	}
	A(){
		this(100);
	}
	public static void main( String[] args) {
		A a1 = new A();
		
}
}
Output : 100

What is Default Constructor in Java With Example?

Default Constructor: – In The Below Program When You Compile A.java File to A.class File Than the dot class File Automatically Empty the Body Constructor Without Any Arguments that Will get Added. if as a Developer We Do not add Constructor in dot Java File.

Constructor in Java
Default Constructor in Java

Note 1: When An Object With an Argument is Created there is no concept of a Default Constructor, Here Constructor has to be Created by the Programmer’s Explicity. Hence the below Program throws an Error.

public class A  {
public static void main (String[] args )  {
A a1 = new A(100);   // Error
}
}

Example 2

public class A  {
A(int x) {
System.out.println(x);
}
public static void main (String[] args )  {
A a1 = new A(100);  // Correct
}
}

Leave a Comment