In this lesson of our Java course, we will learn about the Java constructors. We will see what are Java constructors and why we need them. We will look at some example to understand them more clearly.
Table of Contents
1. Java Constructors
Java constructors are special methods used to create and initialize objects of a class. A Java constructor has the same name as the class and is called when an object of that class is created. Constructors are important in Java programming because they allow for the creation of objects with default values, as well as the ability to set initial values for object properties.
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Person person = new Person("John", 36);
System.out.println("name is "+person.name);
}
}
In this example, the Person
class has a constructor that takes a name
and an age
parameter, and sets the corresponding object properties.
Output
name is John
2. Java Constructors Types
On a high level, we can classify the Java constructors in the following 3 categories:
- Default Constructor.
- Parameterized Constructor
- No-Arg Constructor
Let’s learn about these 3 different Java constructors in more details:
3. Default constructor
A default constructor is a constructor that takes no parameters. It is automatically generated by the compiler if no constructors are defined for a class. The default constructor initializes object properties to their default values. Here is an example of a default constructor in Java:
public class Person {
private String name;
private int age;
public static void main(String[] args) {
Person person = new Person();
System.out.println("name is "+person.name);
System.out.println("age is "+person.age);
}
}
//output
name is null
age is 0
In the above example, we did not specify any constructor but we still use the new keyword to create an instance for the class. This is possible since Java compiler will create a default constructor for us. Here is the de-compiled version from the .class
file generated by compiler
public class Person {
private String name;
private int age;
public Person() {
}
public static void main(String[] args) {
Person person = new Person();
System.out.println("name is " + person.name);
System.out.println("age is " + person.age);
}
}
4. No Argument constructor
A no-argument constructor is a constructor that takes no parameters and is defined explicitly by the programmer. The no-argument constructor initializes object properties to their default values or to any other specified values.
public class Person {
private String name;
private int age;
public Person() {
this.name = "John Doe";
this.age = 30;
}
}
In this example, the Person
class has a no-argument constructor that initializes the name
property to "John Doe"
and the age
property to 30
.
5. Parameterized constructor
A parameterized Java constructor is a constructor that takes one or more parameters. It is used to initialize object properties to specified values.Here is an example of a parameterized constructor in Java:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name) {
this.name = name;
}
public static void main(String[] args) {
Person person = new Person("John", 40);
Person person1 = new Person("David");
System.out.println("Name= "+person.name+ " Age is "+person.age);
System.out.println("Name= "+person1.name);
}
}
In this example, the Person
class has a parametrised constructor that takes a name
and an age
parameter and sets the corresponding object properties. The other parametrised constructor only name as input parameter.
Output:
Name= John Age is 40
Name= David
6. Constructor Key points
Let’s take a closer look at some important points on the Java constructors.
- A Java constructor does not have any return type.
- The constructor name must be the same as the class name.
- Constructor can take parameter similar to the Java method taking input parameters.
- Constructors are the first to be called in the class even before any method of the class can be called.
- If the class does not have any constructor, Java compiler will generate one for us.
- We can’t define constructor as
abstract
,static
orfinal
. - We can overload but can’t override a constructor.
- When we create an object of a class, the constructor for that class is called implicitly.
7. Constructor chaining
In Java constructors, chaining is the process of calling one constructor from another constructor within the same class. This is useful when there are multiple constructors that need to perform the same initialization logic.
public class Person {
private String name;
private int age;
public Person() {
this("John Doe", 30);
}
public Person(String name) {
this(name, 30);
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
In this example, the Person
class has three constructors. The first constructor calls the second constructor, which calls the third constructor. This allows the same initialization logic to be used across multiple constructors.
8. Constructor overloading
Constructor overloading is defining multiple constructors for a class with different parameter lists. This is useful when different initialization logic is needed for different scenarios.
public class Person {
private String name;
private int age;
private String gender;
public Person() {
this.name = "John Doe";
this.age = 30;
this.gender = "Unknown";
}
public Person(String name) {
this.name = name;
this.age = 30;
this.gender = "Unknown";
}
public Person(String name, int age) {
this.name = name;
this.age = age;
this.gender = "Unknown";
}
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
9. Copy constructor
A copy constructor is a constructor that takes an object of the same class as a parameter and creates a new object with the same property values as the original object. This is useful for creating a new object that is a copy of an existing object.
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
}
In this example, the Person
class has a copy constructor that takes a Person
object as a parameter and creates a new Person
object with the same name
and age
properties.
Summary
Java constructors are an important part of Java programming, as they are used to creating and initialize objects of a class. There are several types of constructors, including default constructors, no-argument constructors, parametrised constructors, and copy constructors. Constructor chaining and constructor overloading are also commonly used techniques in Java programming.
When working with Java constructors, it is important to follow recommended guidelines. These include setting appropriate default values, avoiding overly complex constructors, and eliminating unnecessary duplicate code. By using constructors effectively, Java programmers can create well-structured, efficient, and maintainable code. As always, the source code for this article is available on our GitHub repository.