Java Constructors: Initialization and Usage Explained
Table of Content:
Constructor
A constructor is a special kind of method that determines how an object is initialized when it’s created.
Rules for creating Java constructor
There are basically three rules defined for the constructor.
- Constructor name must be same as its class name
- Constructor must have no explicit return type. i.e Constructors do not have a return type — not even void
- Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
Types of Java constructors
There are two types of constructors:
- Default constructor (no-arg constructor)
- Parameterized constructor
Java Default Constructor
A constructor that have no parameter is known as default constructor.
Syntax of default constructor:
ClassName() { }
Example of Default Constructor
In this example, we are creating the no-arg constructor in the Sample class. It will be invoked at the time of object creation.
class Sample{ Sample(){ // constructor System.out.println("this is inside constructor"); } public static void main(String args[]){ Sample b=new Sample(); } }
Output:
this is inside constructor Press any key to continue . . .
Rule: If there is no constructor in a class, compiler automatically creates a default constructor.
What is the purpose of default constructor?
Default constructor provides the default values to the object like 0, null etc. depending on the type.
Example of default constructor that displays the default values
class Student{ String name; int rollNo; void display(){ System.out.println(rollNo+" "+name); } public static void main(String args[]){ Student s1=new Student(); Student s2=new Student(); s1.display(); s2.display(); } }
Output:
0 null 0 null Press any key to continue . . .
Explanation:
In the above class,you are not creating any constructor so compiler provides you a default constructor.Here 0 and null values are provided by default constructor.
Another Example of constructor
class Demo{ int value1; int value2; Demo(){ // this is constructor value1 = 10; value2 = 20; System.out.println("Inside Constructor"); } public void display(){ System.out.println("Value1 === "+value1); System.out.println("Value2 === "+value2); } public static void main(String args[]){ Demo d1 = new Demo(); d1.display(); } }
Output:
Inside Constructor Value1 === 10 Value2 === 20 Press any key to continue . . .
Java parameterized constructor
A constructor that have parameters is known as parameterized constructor.
Why we use parameterized constructor?
Parameterized constructor is used to provide different values to the distinct objects.
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
class Student{ String name; int rollNo; Student(String n, int i){ name = n; rollNo = i; } void display(){ System.out.println(rollNo+" "+name); } public static void main(String args[]){ Student s1=new Student("Dhinchak Pooja", 1); Student s2=new Student("Hero Alom",2); s1.display(); s2.display(); } }
Output:
1 Dhinchak Pooja 2 Hero Alom Press any key to continue . . .