Constructors are used for the purpose of object initialization. Once we create an object, it is a good practice to initialize the object. Then only the object is in a position to respond properly.
It is not considered a good practice to initialize an object at the time of declaration or inside the instance block. Because doing so will initialize every new object with the same values. And we want to initialize different objects with different values.
A constructor is the portion of the code which gets automatically executed when we create objects.
Hence, the main purpose of a constructor is the initialization of the object and not the creation of the object.
Example of Constructor
// class Student public class Student { // properties/characteristics of student. (Name and Roll Number) String name; int roll_no; // Constructor for initializing Student object Student(String name, int roll_no) { // Performing initialization this.name = name; this.roll_no = roll_no; } // static main method public static void main(String[] args) { // creating 2 objects of Student Student student1 = new Student("Meet", 1501); Student student2 = new Student("Preet", 1502); // Printing name and roll number of both students System.out.println(student1.name + " " + student1.roll_no); System.out.println(student2.name + " " + student2.roll_no); } }
In the above code, student constructor is defined to initialize every student object with its name and roll number that are passed as arguments to the constructor.
Output:
Meet 1501 Preet 1502
Difference between Constructor and Instance Block
The main purpose of a constructor is to perform initialization of an object. But, other than that, if we want to perform any activity for every object creation, then we should use instance blocks.
Both constructor and instance block have their own purposes. And replacing them with each other may not always work.
Both constructor and instance block will be executed for every object creation. But first, instance block will be executed and then, the constructor will be executed.
Below program is to print the total number of objects created for a particular class.
//Main class class Demo { // initially, total number of objects created are zero static int total_objects = 0; { // incrementing count value for every new object creation inside instance block total_objects++; } // no arguments constructor Demo() { System.out.println("no arguments constructor called"); } // constructor with an integer argument Demo(int i) { System.out.println("integer argument constructor called"); } // constructor with a String argument Demo(String s) { System.out.println("String argument constructor called"); } public static void main(String[] args) { // creating 3 different objects of Demo class Demo d1 = new Demo(); Demo d2 = new Demo(5); Demo d3 = new Demo("Meet"); // printing total number of objects created System.out.println("Total no of objects created: " + total_objects); } }
We are incrementing count value inside instance block only and instance block perfectly suits in this program.
We can also increment the count value inside the constructor. But that approach is not feasible since we have to repeat same statement in every constructor which unnecessarily increase the length of the program.
Output:
no arguments constructor called integer argument constructor called String argument constructor called Total no of objects created: 3
Rules for defining constructors
- Name of the class and name of the constructor must be identical.
- Since JVM executes constructors automatically when we create objects, they must not have any return type.
Note: If we specify a return type for a constructor, then it will not be constructor anymore and JVM will treat it as a regular method.
- The only applicable modifiers for constructors are public, private, protected and default. If we try to use any other modifier, we will get a compile-time error.
Consider following example.
//Demo class class Demo { // static construtor static Demo() { System.out.println("static constructor called"); } }
Here, we tried to define the constructor as static and it gives us a compile-time error saying modifier static not allowed here.
This is all about Constructors in Java. Hope you find the tutorial helpful.