Constructors
First of all what is a constructor
We can use constructor when creating an object of a class. It’s like methods but not.
Is the constructor name same as the class name ?
yes! it is.
Lets talk about what constructors can do. They can have access modifiers, take arguments but cannot have a return type. It also cannot be marked as static, final or abstract
There are few types of constructors. They are No argument constructor, Default constructor, parameterized constructor and Private constructor.
Default constructor is a No-arg constructor. How? imagine you don’t creates any constructor then java compiler automatically creates a No-arg constructor. This No-arg constructor is called as Default constructor. Every class has a constructor if you creates a constructor no problem, if you not automatically creates a Default constructor.
Get Swapna Munasinghe’s stories in your inbox
Join Medium for free to get updates from this writer.
class Animal{
}
This is the class you created, but the actual class is
class Animal{
Animal(){}
}
Then lets talk about parameterized constructor, this constructor takes one or more parameters. Related values of the parameters must pass during object creation.
If we talk about Private constructors They restricts the ability to create instance of the class from outside the class it self. this is a constructor declared with the private access modifier.








