Java Constructor
Java Constructors
In Java, a constructor Is similar to a method but does not have any return type.
Create a constructor
Here’s how we create constructors:
Class Bicycle {
// constructor
Bicycle() {
// body
}
Here, the constructor Is:
Bicycle() {
// body
}
Things to notice:
A constructor has the same names as the class name.
It doesn’t have any return type.
Why constructors?
When we create an object, a constructor is automatically called. That’s why it is commonly used to initialize fields of an object with default values.
Let’s see a working example of a Constructor next.