Constructor
A constructor is a special method that runs automatically when an object is created, to initialise its fields. In Java and C++ it has the same name as the class and no return type.
If a class defines no constructor, the compiler provides a default one with no parameters. Classes can have several constructors with different parameters (constructor overloading), and one constructor can call another, for example with this(...) in Java.
class Student {
String name;
int rollNo;
Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
}
Student s = new Student("Asha", 101);