Java Constructor
In Java, a constructor is a special method used to create objects of a class.
When the new keyword is used to create an object, the constructor is automatically called to initialize the object's properties.
A constructor has the following characteristics:
-
Same name as the class: The name of the constructor must be exactly the same as the class name, including case. This is a basic requirement of a constructor.
-
No return type: A constructor has no return type declaration, not even
voidis written. This distinguishes it from ordinary methods. -
Automatic invocation: Each time
newis used to create an object, the constructor is automatically called to initialize the object's properties and state. -
Cannot be called directly: A constructor can only be called through the
newkeyword when creating an object, and cannot be called directly like an ordinary method. -
Supports overloading: You can define multiple constructors for a class as long as their parameter lists differ. Through overloading, different constructors can be created to suit different initialization needs.
-
Default Constructor: If no constructor is defined, Java provides a no-argument default constructor. But once any other constructor is defined, Java no longer provides the default constructor.
-
thisUsage of the keyword: In a constructor, you can use thethisto reference the current object's properties and methods, or to call another constructor (must be the first line of the constructor), to avoid duplicate code. -
Cannot be inherited, but can be called: A constructor cannot be inherited by a subclass, but the subclass can use the
super()to call the parent class's constructor, in order to initialize the inherited properties. -
Object Initialization Guarantee: The main role of a constructor is to initialize the object's properties and state, ensuring the object is in a valid initial state when created.
Role of the Constructor:
- Initialize object properties: The main role of a constructor is to assign initial values to the object's properties.
- Ensure the integrity of object initialization: In the constructor, you can set default values or required parameters, thereby avoiding the problem of objects being incompletely initialized.
Types of Constructors
Constructors in Java are divided into two types:No-argument constructorandParameterized constructor.
1. No-argument constructor (default constructor)
If a class defines no constructor, Java provides a no-argument constructor by default. For example:
Example
public
class
Person
{
public
Person
(
)
{
System
.
out
.
println
(
"Person object created"
)
;
}
}
- When no constructor is explicitly defined, Java automatically provides a default no-argument constructor.
- Once other constructors are defined, Java will no longer provide the default constructor.
2. Parameterized constructor
You can define a constructor with parameters, used to assign values to properties when creating an object:
Example
public
class
Person
{
String
name
;
int
age
;
public
Person
(
String
name,
int
age
)
{
this
.
name
=
name
;
this
.
age
=
age
;
}
}
When calling a parameterized constructor, you can initialize the object's properties:
Person p = new Person("Alice", 25);
Constructor Overloading
Java supports constructor overloading, i.e., you can define multiple constructors in the same class as long as their parameter lists differ. For example:
Example
public
class
Person
{
String
name
;
int
age
;
public
Person
(
)
{
this
.
name
=
"Unknown"
;
this
.
age
=
0
;
}
public
Person
(
String
name
)
{
this
.
name
=
name
;
this
.
age
=
0
;
}
public
Person
(
String
name,
int
age
)
{
this
.
name
=
name
;
this
.
age
=
age
;
}
}
When creating an object, Java automatically selects the matching constructor based on the number and type of arguments passed:
Person p1 = new Person(); // 调用无参构造方法
Person p2 = new Person("Alice"); // 调用单参数构造方法
Person p3 = new Person("Bob", 30); // 调用双参数构造方法
The this Keyword in Constructors
In a constructor, the this keyword is typically used in two situations:
** 1. Reference the current object's properties or methods:** When the constructor's parameter name is the same as the class property name, use this to distinguish the class property from the parameter.
Example
public
Person
(
String
name,
int
age
)
{
this
.
name
=
name
;
// this.name refers to the class property
this
.
age
=
age
;
}
**2. Call another constructor:** You can use this() to call other constructors of the current class, commonly used to avoid duplicate code, but it must be placed on the first line of the constructor.
Example
public
Person
(
String
name
)
{
this
(
name,
0
)
;
// Call another two-parameter constructor
}
public
Person
(
String
name,
int
age
)
{
this
.
name
=
name
;
this
.
age
=
age
;
}
Constructors are a very important part of Java object-oriented programming. By using constructors, you can effectively control the object initialization process and ensure the integrity and consistency of the created object's state.