Constructors in Java

Java Constructors are used to initializing the object. An object is a program memory (RAM memory). The constructor looks like a method. However, there are some differences between a constructor and method.

Difference between Constructor and Method

The following are the differences between a constructor and a method.

java constructors

Java Constructors - Default Constructor

Constructor without any argument is called default constructor and maximum one default constructor is allowed in a single class. The default constructor is the constructor that is invoked by JVM in the first place. Java compiler automatically provides the default constructor if a user has not defined it explicitly. This constructor is used to initialize a single value to all object.

public class Test {
	static void method(){
		System.out.println("Test method");
	}
	Test(){
		System.out.println("Test() constructor");
	}
public static void main(String[] args) {
	System.out.println("In main method");
	method(); //calling method directly
	Test.method(); //calling method by class name
	new Test(); //calling constructor using new keyword
}
}
//OUTPUT
/*In main method
Test method
Test method
Test() constructor
*/

Java Constructors - Parameterized Constructor

Constructor with an argument is known as parameterized constructor. Multiple numbers of parameterized constructors are allowed for a single class. This constructor is used to initialize different value to different object.

public class Test {
	
	Test(int x){
		System.out.println("Test() parameterized constructor");
	}
	
public static void main(String[] args) {
	System.out.println("In main method");
	new Test(6); //calling constructor using new keyword
	new Test(); //ERROR,
	//since when parameterised constructor is present user needs
	//to explicitly define the default constructor.
}
}

Note: As javac never provides any default constructor if the user has already defined any parameterized constructor, so calling to default constructor without defining it will be a compilation error.

Java Constructors - Private Constructor

If the constructor declared with the private keyword then it is known as a private constructor. If the constructor is private then it cannot be called from outside the class. In other words, the class can't be instantiated from outside of that particular class.

Java Constructors - Copy Constructor

Copy constructors are mainly used to create duplicate objects which are known as cloned objects. Duplicate objects have same characteristics as an original object but they occupy different memory to that of an original object. A copy constructor is a constructor that takes only one argument whose type is same as the class it is in.



public class Test {
int roll;
String name;
Test(){
	System.out.println("In default constructor");
}
//copy constructor
Test (Test ob){
	System.out.println(ob);
	roll=ob.roll;
	name=ob.name;
}
public static void main(String[] args) {
	Test obj1=new Test();
	obj1.roll=70;
	obj1.name="Brad";
	System.out.println(obj1.roll+"\t"+obj1.name);// original object
	Test obj2=new Test(obj1);
	System.out.println(obj2.roll+"\t"+obj2.name); //value of duplicated object initially
	obj2.name="Formulae Stack";
	obj2.roll=20;
	System.out.println(obj2.roll+"\t"+obj2.name); //value of duplicated object after change
	System.out.println(obj1.roll+"\t"+obj1.name); //original object
	
}
}

//OUTPUT

/*In default constructor
70	Brad
[email protected]
70	Brad
20	Formulae Stack
70	Brad
*/

Notes from the above program:

  • At first, the object reference 'ob' points to name and roll whose values initially are null and 0.
  • Second, the object reference 'obj1' points to name and roll whose values are Brad and 70.
  • Third, the object reference 'obj2' points to name and roll whose values initially are Brad and 70.
  • Fourth, the object reference 'obj2' points to name and roll whose values are Formulae Stack and 20.

A pictorial representation to understand the above situation

java constructors