Java constructors or constructors in Java are terminologies used to construct something in our programs. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
At least one constructor is called every time an object is created with the new() keyword.
If no constructor is available in the class, it uses the default constructor. In this case, the Java compiler automatically creates a default constructor.
Rules for creating Java constructor:
The constructor is governed by two rules:
- The constructor’s name must match the class name.
- There must be no explicit return type in a constructor.
- An abstract, static, final, and synchronized Java constructor is not possible.
Types of Java constructors:
There are two types of constructors in Java:
- Default constructor (no-arg constructor)
- Parameterized constructor
Default constructor (no-arg constructor):
- The default constructor is a constructor that has no parameters.
- If a constructor is not defined in a class, the compiler creates a default constructor for the class (with no arguments).
- The compiler does not create a default constructor if we write a constructor with arguments or no arguments.
- Note: The default constructor is used to give the object default values such as 0, null, and so on, depending on the type.
Syntax of default constructor:
class_name()
{
}
Example:
//Java Program to create and call a default constructor
class Demo{
//creating a default constructor
Demo(){System.out.println("Default constructor is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Demo b=new Demo();
}
}
Output:
Default constructor is created
Example of default constructor that displays the default values:
//Let us see another example of default constructor
//which displays the default values
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
}
}
Output:
0 null
0 null
Parameterized constructor:
- Parameterized constructors are constructors that have parameters.
- If we want to initialize fields of the class with our own values, then use a parameterized constructor.
- Note: The parameterized constructor is used to provide different values to distinct objects.
Example:
//Java Program to demonstrate the use of the parameterized constructor.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){
System.out.println("ID: "+id+" Name: "+name);
}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Suraj");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Output:
ID: 111 Name: Suraj
ID: 222 Name: Aryan
Constructor Overloading in Java:
It is sometimes necessary to initialize an object in multiple ways. Constructor overloading can be used to accomplish this.
In Java, constructor overloading is the practice of having multiple constructors with different parameter lists.
Example of Constructor Overloading:
//Java program to overload constructors
class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
}
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}
Output:
111 Karan 0
222 Aryan 25
Note: also read about the Methods in Java
Follow Me
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment