What are Constructors in Java?

Опубликовано: 31 Март 2026
на канале: Software Testing Hacks
7,996
100

• A constructor in Java is similar to a method that is invoked when an object of the class is created.

• A constructor has the same name as that of the class and does not have any return type.

Example:
class Test {
Test() {
// constructor body
}
}

Rules for creating a Constructor :
1. The class name and constructor name should be the same.
2. It must have no explicit return type.
3. It can not be abstract, static, final, and synchronized.

Types of Constructors in Java
1. No-Arg Constructors/ Non-Parameterized Constructors
2. Parameterized Constructors

No-Arg Constructors
As the name suggests, these constructors do not have any arguments since they are created by default in Java when no constructors are passed

Parameterized Constructor in Java
In this case, the programmer will pass the constructors with one or more arguments.
#java #javaprogramming