constructor in java

constructor in java

The constructor is the method who's name is same as class name.
when the class is run the constructor also run

ref‮re‬ to:lautturi.com
class MyClass {
  public MyClass () {
    //constructor code
  }
}

example:

class Main {
  private String name;

  // constructor
  Main() {
    System.out.println("Constructor Called:");
    name = "Lautturi";
  }

  public static void main(String[] args) {

    // constructor is invoked while creating an object of the Main class
    Main obj = new Main();
    System.out.println("The name is " + obj.name);
  }
}
Created Time:2017-09-03 13:07:04  Author:lautturi