Hum.. I always forget the function of a constructor because I don’t have the custom to use.
For this reason, I will put here some tips:
A constructor is a method with instructions that will execute ALWAYS when will instantiated a object of this class.
Like this:
- Class MyClass.java
public MyClass() { //this is the constructor method
System.out.println(“Hello World !”);
} }
So… when you call this class, the constructor will be execute.
- Class Test.java
public static void main(String args[]) {
MyClass Obj1 = new MyClass();
}
}