Course Content
Java Extended
Java Extended
Constructor
How to simplify class initializing?
Initializing each field every time can be cumbersome. For such purposes, classes have constructors. The syntax for a constructor is as follows:
Main
modifier ClassName(ParameterType parameter1, ParameterType parameter2) { this.parameter1 = parameter1; this.parameter2 = parameter2; }
Let's go through each word written here:
modifier
: This refers to the access modifier, which is oftenpublic
for constructors. Cases where a constructor can have a different access modifier, will be covered in a separate course;ClassName
: This is simply the name of the class in which you are creating this constructor;ParameterType
: This is the type of parameter that will be used in the constructor. It is important to pay attention here because this parameter should be the same type as the field in the class; For example: If the class has two parameters,String name
andint age
, then the constructor should have the same parameters (if we want to initialize all fields through the constructor). If we want to initialize only a specific class field through the constructor, for example,name
, then we should use only one parameter with the same type and name as the field in the class;- Next, inside the constructor body, we assign values to the class fields using the values passed in the parameter.
"this" keyword
With the help of this
keyword, we can refer to the class field in which we write the value of this keyword. For example, when we have a class with two fields: name
and age
, we can write this.name
inside the constructor or method, and it will specifically refer to the class field, not the local variable that is passed as a parameter to that constructor or method. This way, we can initialize class fields through the constructor using the syntax: this.name = name;
.
Example
Let's take a look at an example of a constructor in a class that will help us initialize all the fields to make it clearer:
Person
class Person { String name; int age; String gender; public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } }
We have a class called Person
with three fields. Each of these fields has been added to the constructor using the keyword this
. Therefore, the constructor initializes the fields with the values from the parameters. Let's use this constructor in the main
method so that you can see that the fields are initialized with what we pass as parameters:
Main
package com.example; class Person { String name; int age; String gender; public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } } public class Main { public static void main(String[] args) { Person bob = new Person("Bob", 20, "Male"); System.out.println("Bob's name: " + bob.name + ", Bob's age: " + bob.age + ", Bob's gender: " + bob.gender); } }
We initialized the Person
object bob
using the constructor by passing the name
, age
, and gender
as parameters. Then we printed its parameters to the screen and see that the object has field values as specified in the constructor parameters.
Constructor overload
The constructor can also be overloaded and not cover the initialization of all class fields. For example, if we don't want to specify Alice's gender, we can overload the constructor to accept only 2 parameters and initialize the fields with them:
Main
package com.example; class Person { String name; int age; String gender; public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public Person(String name, int age) { this.name = name; this.age = age; } } public class Main { public static void main(String[] args) { Person bob = new Person("Bob", 20, "Male"); System.out.println("Bob's name: " + bob.name + ", Bob's age: " + bob.age + ", Bob's gender: " + bob.gender); Person alice = new Person("Alice", 17); System.out.println("Alice's name: " + alice.name + ", Alice's age: " + alice.age); } }
It can be concluded that a constructor, like a method, can be overloaded and accept different numbers and types of parameters.
Default constructor
A default constructor is a constructor that takes no parameters and does not initialize any fields. It exists in all classes by default if they do not have any other type of constructor, which is why it is called the default constructor. Every time we create an object of any class, we use a constructor. In order to use the default constructor when we already have a constructor that accepts parameters, we also need to write an empty constructor:
Main
package com.example; class Person { String name; int age; String gender; public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public Person(String name, int age) { this.name = name; this.age = age; } public Person() { } } public class Main { public static void main(String[] args) { Person bob = new Person("Bob", 20, "Male"); System.out.println("Bob's name: " + bob.name + ", Bob's age: " + bob.age + ", Bob's gender: " + bob.gender); Person alice = new Person("Alice", 17); System.out.println("Alice's name: " + alice.name + ", Alice's age: " + alice.age); Person john = new Person(); System.out.println("John's name: " + john.name + ", John's age: " + john.age); } }
We use 3 different constructors to initialize each of the Person
objects. As we can see in the last example, John
does not have a name or age because these fields are not initialized for the object. Thus, we can overload the constructor as many times as we need and create objects through it.
Thanks for your feedback!