hamburger

Difference Between Constructor and Method in Java

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Difference between Constructor and Method: Java programming language has OOP as its basic concept for the construction of programs. Class is the building block of such language. All the concepts are encapsulated within a class. Constructor and method are used to determine the way class data shall be used. The major difference between constructor and method is that constructor is called when the creation of an instance of the object is done.

The method in Java is used for the exhibition of the functionality of the object whereas the Constructor in Java is used for the initialization of the object. Here, we will learn about constructors and methods and along with it, we will discuss the difference between constructor and method based on various factors.

Difference Between Constructor and Method

In OOP both constructor and method play an important role in the execution of a program. There exists a lot of differences between constructor and method which are explained in the table provided below on various factors.

Key Differences Between Constructor and Method

Constructor Method
Initialization of the object is done by constructors. The functionality of the object is done by the method.
Implicitly invoked.  Invoked during the run of the program.
Not inherited by the subclass. A subclass inherits the method.
Can not have a different name than the class. Can have a different name than the class.
The keyword
ew is used to invoke.
Invoked without a new keyword and during the program through method calls.
Many constructors can exist with different parameters. Many methods can exist but should have different parameters.

What is the Constructor in Java?

The constructor is a Java programming language that is used for the initialization of the state of an object. They contain functions and data members similar to the method in Java. They are executed in the creation phase of the object. It carries a collection of statements. The statements or instructions are executed at the creation phase.

Example of Constructor in Java:

[Java Program to illustrate constructor]

public class Main {
int x;

public Main(int y) {
x = y+1;
}

public static void main(String[] args) {
Main myObj = new Main(6);
System.out.println(myObj.x);
}
}

Output: 7

What is the Method in Java?

The Methods in Java are the set of codes. They are called whenever any specific task is to be performed. They can have a name different from the class. It is used for indicating the specific behaviour of the object which is used for the execution of that task. The method can be abstract, static, final, and synchronized.

Example of Method in Java:

[Java Program to illustrate method]

public class Main {
static void myMethod() {
System.out.println(This is an example of a method in Java);
}

public static void main(String[] args) {
myMethod();
}
}

Output: This is an example of a method in Java.

Check out a few important articles related to the difference between constructor and method in the table provided below:

Difference Between Python and C++ Difference between algorithm and flowchart 
Difference Between Constructor and Method in Java Difference between C and Java
Difference between Interface and Class in Java Difference between circuit and packet switching
Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium