Java Constructors
A constructor is block of code which is invoked when an object of a class is created using the new keyword. A constructor is used to initialize objects. A constructor can also be used to set initial values to the instance variables of a class.
A constructor name must always match with the class name. However, a constructor is syntactically similar to a method but it has no return type.
A constructor can be default or parameterized.
Default Constructor
A default constructor is a constructor without parameters. Every Java class has a public default constructor by default, meaning even if you do not create a constructor for a class explicitly, Java will automatically create a public default constructor that has no parameters. This default constructor of a class is called when you created an object of that class using the new keyword without passing parameters.
Example
public class Bird {
//constructor without parameters
public Bird() {
}
}
Here, the constructor is declared with public access modifier which indicates that the constructor can be accessed from other classes.
Parameterized Constructor
Parameterized constructors in Java are constructors with parameters. Parameterized constructors are generally used to initialize variables and objects.
Example
public class Dog {
public String name;
public String color;
public double weight;
//constructor with parameters
public Dog(String name, String color) {
this.name = name;
this.color = color;
}
}
Here is an example of a ConstructorExample class with a dog object created using the parameterized constructor of the Dog class:
public class ConstructorExample {
public static void main(String[] args) {
Dog dog = new Dog("Bruno", "brown");
System.out.println("The dog name is " + dog.name +
". It's color is " + dog.color);
}
}
Output
Multiple Constructors
A class can have any number of constructors.
Here is an example of a User class with one default and four parameterized constructors:
Example
package com.example;
public class User {
private String firstName;
private String lastName;
private String email;
private int age;
public User() {
System.out.println("Creating object with default constructor");
}
public User(String firstName) {
System.out.println("Creating object with firstName intialized");
this.firstName = firstName;
}
public User(String firstName, String lastName) {
System.out.println("Creating object with firstName, lastName intialized");
this.firstName = firstName;
this.lastName = lastName;
}
public User(String firstName, String lastName, String email) {
System.out.println("Creating object with firstName, lastName, email intialized");
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public User(String firstName, String lastName, String email, int age) {
System.out.println("Creating object with firstName, lastName, email, age intialized");
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.age = age;
}
}
In the following example, we are creating five user objects using different constructors:
Example
package com.example;
public class UserExample {
public static void main(String[] args) {
User user1 = new User();
User user2 = new User("Danny");
User user3 = new User("Danny", "Kent");
User user4 = new User("Danny", "Kent", "[email protected]");
User user5 = new User("Danny", "Kent", "[email protected]", 20);
}
}
Output
Creating object with firstName intialized
Creating object with firstName, lastName intialized
Creating object with firstName, lastName, email intialized
Creating object with firstName, lastName, email, age intialized
Important Points to Remember
- A Constructor is not considered as a member of a class.
- Constructors can be overloaded, which means you can provide more than one constructor for a class if each constructor has a unique signature. Constructor overloading is having of more than one constructor with different number of parameters list, in such a way that each constructor is initialized to perform different task.