- A constructor in Java is a special block of code that is automatically invoked when a new object of a class is created. Its primary purpose is to initialize the state or attributes of that newly created object.
Key Characteristics
Same Name: The constructor's name must be exactly the same as the class name.
No Return Type: It cannot have a return type, not even void.
Automatic Invocation: It triggers implicitly when you use the new keyword.
No Overriding: It can be overloaded but cannot be overridden.
Constructor is called automatically when objects are created it's an default behaviour.
Constructor doesn't need return type.(No need of return type).
It initializes object specific values. It assigns specific values for specific objects.
Top comments (0)