Constructor java là gì

     

In this tutorial, we will learn about the constructor of an object, how khổng lồ create them, what is the difference between the default constructor & the constructor we define & final what is the overloaded constructor?

Definition of the constructor

In Java, the constructor is a special method, which is used to initialize & return the object of the class khổng lồ which it is defined. The constructor will have the same name as the class it defines, and they vị not define a return value.

Bạn đang xem: Constructor java là gì

For example:


package com.hoanganhmotel.com;public class Student private String name; private int age; public Student(String name) this.name = name;

In the example above, we have defined a constructor for the Student object, as you can see that it has no return type and that its name is the same as the class khổng lồ which it is defined.

When an object is initialized by calling its constructor with the new operator, it invokes the superclass constructor và all instance variables are initialized with their mặc định values. Back lớn the above example, when initializing the Student object by calling:


package com.hoanganhmotel.com;public class Example public static void main(String<> args) Student student = new Student("Khanh");

then the value of the age variable in the Student object will have a default value of type int khổng lồ 0.

Default constructor

If in an object we bởi not define a constructor, by default, Java will add a default constructor khổng lồ our object.

For example, I defined the Student object as follows:


package com.hoanganhmotel.com;public class Example public static void main(String<> args) Student student = new Student();

Obviously, although I vì chưng not define any constructors in the Student object, I can still initialize this object.

The mặc định constructor does not contain any parameters, and when called lớn initialize an object, it also calls the constructor of the superclass & initializes the instance variables.

In the case, we have defined a constructor for the object:


package com.hoanganhmotel.com;public class Student private String name; private int age; public Student(String name) this.name = name;

Java will not automatically showroom the default constructor, and then if we try lớn initialize the object using the mặc định constructor, then the compile error will happen:

*

Defines a constructor

We can define a constructor for any object so that all properties of the object include methods, instance variables can be called according khổng lồ our definition.

Xem thêm: Cách Đơn Giản Để Xử Lý Bàn Phím Macbook Không Gõ Được Chữ : Cách Sửa Nhanh

For example:


package com.hoanganhmotel.com;public class Student private String name; private int age; public Student(String name) this.name = name; age = 30;

We can use four access modifiers khổng lồ define a constructor, and thus we can limit the scope of object access from other objects.

What happens if we define a constructor with a return value type? As such, Java treats it as a normal method rather than as a constructor.

For example:


package com.hoanganhmotel.com;public class Student private String name; private int age; public void Student(String name) this.name = name; age = 30;

The Student object now has only a default constructor added by Java. If we initialize the Student object with a name parameter, we get an error.

*

Overloaded constructor

What is an overloaded constructor? That is when we define multiple constructors for an object and each constructor will have different parameters in terms of both the parameter number & the data type of the parameter.

For example:


package com.hoanganhmotel.com;public class Student private String name; private int age; public Student(int age) this.age = age; public Student(String name) this.name = name; public Student(String name, int age) this.name = name; this.age = age;

The rule to define an overloaded constructor is:

Constructors, as they say, must be defined using different parameters in terms of both the number of arguments và the type of the parameter.The constructors are not defined differently only in the access modifier.

In the case where the object has multiple constructors và we want to hotline this constructor from another constructor then we must use the từ khoá this:


package com.hoanganhmotel.com;public class Student private String name; private int age; public Student(int age) this.age = age; public Student(String name) this.name = name; public Student(String name, int age) this(name); this.age = age;

This statement to điện thoại tư vấn this other constructor must be in the first line of the constructor, if it is after any statement, the code will be compiled error:

*


*
report this ad


No search term specified. Showing recent items.

Xem thêm: Đồng Hồ Vạn Năng Là Gì? Cách Sử Dụng Đồng Hồ Vạn Năng Kim Chi Tiết A

Search or use up and down arrow keys lớn select an item.