Constructor vs method in java

 

          Constructor              Method
  1. A Constructor has same name as of class.
  2. A Constructor invoked implicitly.
  3. A Constructor must not have any explicit return type.
  4. A Constructor is used to initialize the object’s state.
  1. A Method may or may not have same name as of class.
  2. A Method invoked explicitly.
  3. A Method must have a explicit return type.
  4. A Method is used to show object’s behavior.

Note: An object can also perform any operation like any other method.

Example

public class Main
{
    Main(){
        System.out.println("Constructor Called");
    }
 
    public void show(){
         System.out.println("Method Called");
    }
	public static final void main(String[] args) {
	    Main obj1 = new Main();
	    obj1.show();
	}
}

Output

Constructor Called
Method Called

Java interview questions on constructor

Please Share