Differences between a Constructor and a Method in Java

DifferencesJavaObject Oriented ProgrammingProgramming

Constructor

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.

class className {
  // constructor
   className(){}
}

Method

A Java method is a collection of statements that are grouped together to perform an operation. A method is part of a class and is used to provide option o execute statements to an object. A method cannot have same name as that of the class.

class className {
   // method
   public void doSomething(){}
}

Differences

Following are the important differences between Constructor and Method.

Sr. No. Key Constructor Method
1 Purpose Constructor is used to create and initialize an Object. Method is used to execute certain statements.
2 Invocation A constructor is invoked implicitly by the System. A method is to be invoked during program code.
3 Invocation A constructor is invoked when new keyword is used to create an object. A method is invoked when it is called.
4 Return type A constructor can not have any return type. A method can have a return type.
5 Object A constructor initializes an object which is not existent. A method can be invoked only on existing object.
6 Name A constructor must have same name as that of the class. A method name can not be same as class name.
7 Inheritance A constructor cannot be inherited by a subclass. A method is inherited by a subclass.

Example

Following example shows the usage of no argument constructor and method and the sequence of their execution −.

JavaTester.java

public class JavaTester { int num; JavaTester(){ num = 3; System.out.println("Constructor invoked. num: " + num); } public void init(){ num = 5; System.out.println("Method invoked. num: " + num); } public static void main(String args[]) { JavaTester tester = new JavaTester(); tester.init(); } }

Output

Constructor invoked. num: 3
Method invoked. num: 5

Example

Following example shows the usage of parameterized constructor and method with parameters and the sequence of their execution −.

JavaTester.java

public class JavaTester { int num; JavaTester(int num){ this.num = num; System.out.println("Constructor invoked. num: " + num); } public void init(int num){ this.num = num; System.out.println("Method invoked. num: " + num); } public static void main(String args[]) { JavaTester tester = new JavaTester(3); tester.init(5); } }

Output

Constructor invoked. num: 3
Method invoked. num: 5

Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other start-up procedures required to create a fully formed object. A method can be public which then can be accessed via the object. A method can be private where only the owner object can call that method in other methods.

raja
Updated on 19-Aug-2022 10:39:38

Advertisements