Constructors and this keyword in Java

A constructor is a special method that is executed when you create an object. In the declaration, the difference with the standard methods, is that it has no return type and its name is the same as the class. E.g:

class Person {
	public Person() {   ….   }
            public Person(String name) {   ….   }
}
  

There may be multiple constructors in a class if they have different lists of parameters. Two parameter lists are different if the order and type of the parameters are different.

Constructors and new

Depending on how the object is created, a different constructor will be called.

If we create an object with:

An error will occur if there is no method matching the parameters of the new call.

Empty constructor

The parameterless constructor is called empty constructor. If no constructor is defined, the compiler creates a default constructor. This allows you to create objects without arguments with "new Class ()", although no constructor is defined.

Many frameworks and tools need you to define objects with empty constructors. A common mistake is to add a second constructor and forget that this makes the compiler not to create the empty constructor.

This keyword

 

A member method of an object is associated with the object. When the object is running, we can use the keyword this, to obtain a reference to the associated object.

Within the method, we can use this.name to access the name of the associated object.

The keyword this, works in the same way inside a constructor.

 


Example with constructors and the keyword this

package com.edu4java.javatutorials;

class Person {
	String name;
	String surname;
	String address;

	public Person() {
		this.name = "not reported";
		this.surname = "not reported";
		this.address = "not reported";
	}

	public Person(String name) {
		this();
		this.name = name;
	}

	public Person(String name, String surname, String address) {
		super();
		this.name = name;
		this.surname = surname;
		this.address = address;
	}

	public String toString() {
		return "Person = " + this.name + " " + surname + " - Address: " + address;
	}

	public static void main(String[] args) {
		Person p1 = new Person();
		Person p2 = new Person("Edu");
		Person p3 = new Person("Pepe", "Garcia", "Gran Via 14");
		System.out.println(p1.toString());
		System.out.println(p2.toString());
		System.out.println(p3.toString());
	}
}

In the toString method we use this to access the name field. As we see in the same line when accessing the surname field, this is not really necessary. By default, the compiler understands that it is the field of the associated object.

	public Person(String name, String surname, String address) {
		this.name = name;
		this.surname = surname;
		this.address = address;
	}

The keyword this is necessary in Person (String name, String surname, String address) where there is a parameter called name.

If we write only "name", the compiler understands that we refer to the parameter and not the field. This is why this.name = name is used;

	public Person(String name) {
		this();
		this.name = name;
	}

Another interesting case where we use the keyword this, is in the constructor Person (String name). This constructor is called to report only the name. We want the other fields to have the default content given by the empty constructor; "not reported". We use this(), to call the empty constructor public Person () from the constructor Person (String name). This fills all fields with "not informed" and then updates the name with the parameter. 

	Person p1 = new Person();
	Person p2 = new Person("Edu");
	Person p3 = new Person("Pepe", "Garcia", "Gran Via 14");
	System.out.println(p1.toString());
	System.out.println(p2.toString());
	System.out.println(p3.toString());

In the main method, we can see how three Person objects are created, using different constructors. After we print each of the objects using the toString () method.