Javatpoint Logo
Javatpoint Logo

Difference Between the Constructor and the Method in Java

As we know that Java strictly follows the OOPs concepts. All the variables and methods must be presented in the classes. Java classes contain both the constructor and the method. Before understanding the difference between constructor and method, let's recall the constructor and method.

Constructor vs Method in Java


The constructor and method both are different from each other. However, the constructor is used for initializing the object's state. Constructors can also contain data members and member functions in the same way as the method contains. The data members and member functions of the constructor are executed at the time of creating an object of that class.

The new keyword plays an important role in creating an instance of the class. Let's take an example and understand how we can create an object of the class and call it the constructor of the class.


Constructor vs Method in Java


The method is a block of code that is used for performing a particular task. It reduces the length and the repetitive code. It may or may not return a value to the caller.

By creating a method, we can reuse the code without retyping the code. Let's take an example of a method to understand how we can create a method and use it in our class.


Constructor vs Method in Java

Difference Between Constructor and Method

The following table shows the key differences between constructor and method.

Sr. No. Constructor Method
1. A block of code that initialize at the time of creating a new object of the class is called constructor. A set of statements that performs specific task with and without returning value to the caller is known as method.
2. It is mainly used for initializing the object. It is mainly used to reuse the code without writing the code again.
3. It is implicitly invoked by the system. A method is called by the programmer.
4. The new keyword plays an important role in invoking the constructor. Method calls are responsible for invoking methods.
5. It has no return type. It can or cannot return any value to the caller. So, it has a return type.
6. The constructor name will always be the same as the class name. We can use any name for the method name, such as addRow, addNum and subNumbers etc.
7. A class can have more than one parameterized constructor. But constructors should have different parameters. A class can also have more than one method with the same name but different in arguments and datatypes.
8. Sub-class cannot inherit parent class constructor. Sub-class can inherit the method of the parent class.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA