What is a Constructor?
A constructor is a special method used to initialize objects. It is called when an object of a class is created.Rules:
public class MyClass {
int x; // Constructor
public MyClass() {
x = 5;
}
public static void main(String[] args) {
MyClass myObj = new MyClass(); // Constructor called
System.out.println(myObj.x); // Outputs 5
}
}