Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Constructor | Classes
Java Extended

ConstructorConstructor

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:

java

Main.java

Let's go through each word written here:

  • modifier: This refers to the access modifier, which is often public 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 and int 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:

java

Person.java

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:

java

Main.java

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:

java

Main.java

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:

java

Main.java

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.

1. Which of the following statements about constructors is true?
2. What is the purpose of a constructor in Java?
3. Which of the following code snippets demonstrates the correct way to create a parameterized constructor?

question-icon

Which of the following statements about constructors is true?

Виберіть кілька правильних відповідей

What is the purpose of a constructor in Java?

Виберіть правильну відповідь

Which of the following code snippets demonstrates the correct way to create a parameterized constructor?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 4. Розділ 6

ConstructorConstructor

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:

java

Main.java

Let's go through each word written here:

  • modifier: This refers to the access modifier, which is often public 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 and int 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:

java

Person.java

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:

java

Main.java

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:

java

Main.java

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:

java

Main.java

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.

1. Which of the following statements about constructors is true?
2. What is the purpose of a constructor in Java?
3. Which of the following code snippets demonstrates the correct way to create a parameterized constructor?

question-icon

Which of the following statements about constructors is true?

Виберіть кілька правильних відповідей

What is the purpose of a constructor in Java?

Виберіть правильну відповідь

Which of the following code snippets demonstrates the correct way to create a parameterized constructor?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 4. Розділ 6
some-alt