Java Constructors

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 take a look at some example to understand them more clearly.

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
Java Constructors

Let’s leanr about these 3 different types of 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 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);
    }
}
Did you know, the default constructor initialized any uninitialized instance variables with default values. In above example the name was initialized to null value while the int was initialized to the default value as 0.

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 parameterized constructor that takes a name and an age parameter, and sets the corresponding object properties. The other parameterized constructor only name as input parameter.

Output:

Name= John Age is 40
Name= David
Remember, when you defined explicit constructor (either no argument or parameterized), compiler will not generate any Java constructors for you. It will be our responsibility to generate the constructors as per our requirement.

6. Constructor Key points

Let’s take a closer look on some important points on the Java constructors.

  1. A Java constructor does not have any return type.
  2. The constructor name must be the same as the class name.
  3. Constructor can take parameter similar to the Java method taking input parameters.
  4. Constructors are the first to be called in the class even before any method of the class can be called.
  5. If class does not have any constructor, Java compiler will generate one for us.
  6. We can’t define constructor as abstract, static or final.
  7. We can overload bit can’t override a constructor.
  8. 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 in turn calls the third constructor. This allows the same initialization logic to be used across multiple constructors.

8. Constructor overloading

Constructor overloading is the process of 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 create and initialize objects of a class. There are several types of constructors, including default constructors, no-argument constructors, parameterized 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 best practices such as providing appropriate default values, ensuring constructors are not overly complex, and avoiding 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.

Scroll to Top