Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Constructor

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:

Role of the Constructor:


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"

)

;

   

}

}

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.