Constructor is also a method/member function of the class. The following may make you feel different about constructor
1. Constructor has the same name as the class name.
class pikachu{
String name;
int level, HP;
pikachu(){
name = new String();
level = 2;
HP = 10;
}
}
2. The constructor is the first method that runs when an instance of the class (also called object) is created.
public static void main(String args[]){
pikachu james = new pikachu();
}
where,
james - an object (technically, identifier/handle) of class 'pikachu'
new - keyword to indicate allocation of memory
pikachu() - calling the constructor
3. The constructor, being the first method to run, is the usually used to instantiate objects and initialize variables, which are the members of the class.
name - string is instantiated
level,HP - initialized
4. Constructor returns no value and hence no return type.
Also,
1. Constructor, like any member function, can be overloaded.
2. Constructors can take all 4 access specifiers.
Deliver2inbox