Difference between constructor and method in Java

DifferencesJava Programming Object Oriented Programming

Classes are the fundamental building blocks of Java. It is a programmable template that can be expanded upon, and it determines the form and characteristics of an item. One may say that a class is the most fundamental component of an object oriented programming language like Java. Each and every idea that will be put into action by means of a Java application must first be encased inside of a class. In object-oriented programming, the fundamental building blocks are called classes objects.

Variables and methods are the building blocks of a Java class. Instance variables are the terms used to refer to the variables that are declared within a class. The use that can be made of a class's data is controlled by its methods, which are collections of lines of code. It is more analogous to a subprogram in the sense that it processes data and returns a value. Constructors are quite similar to methods, with the key difference being that constructors are invoked whenever an instance of an object is created.

What is a Constructor?

Constructors are responsible for initializing objects, although they do not produce any return type. Some people believe that the name of the constructor ought to be the same as the name of the class.

It is possible to overload a constructor, but there is no way to bypass it. Additionally, it must not be concrete, abstract, or actual.

There are three different kinds of constructors used in the Java programming language.

  • No-Arg Constructor − It has been asserted that the Java Constructor might have parameters or it could not have any arguments at all.

  • Parameterized Constructor − Constructors in Java that can take one or more arguments are referred to by their formal name, Java Parameterized Constructor (constructor with parameters).

  • Default Constructor − When no one creates a Constructor, the Java compiler creates one while the program is executing. As a result, it is known as a default Constructor.

Constructor Example in Java

Take a look at the following example,

// Java Program to illustrate constructor
import java.io.*;
class Const_Test {
   int num;
   String name;

   // This would be invoked while an object of that class created.
   Const_Test (){
      System.out.println("Constructor is being called in the program");
   }
}
public class Temp {
   public static void main(String[] args){

      // this would invoke default constructor.
      Const_Test Const_Test1 = new Const_Test();

      // Default constructor provides
      // the default values to the object like 0, null
      System.out.println(Const_Test1.name);
      System.out.println(Const_Test1.num);
   }
}

Output

On execution, it will produce the following output

Constructor is being called in the program
null
0


What is a Method?

Methods in OOP carry out a limited set of responsibilities and duties. Methods cannot have the same name as the class's constructor. Additionally, Methods possess a return type (including void). It is commonly believed that Methods ought to consistently be explicit (Parameterized Constructor).

In contrast to Constructors, the Java compiler does not give any default methods, if the methods have not been constructed by hand. Both their inheritance and their ability to be overridden are characteristics of non-static methods.

Any object (for methods that are not static), a class reference (for methods that are static), or the method's name can be used to invoke it. Additionally, they take a variety of parameter values.

Method Example in Java

Take a look at the following Java program

import java.io.*;
class Adding_two_Nums {
   int sum = 0;
   public int addTwoInt(int a, int b){
      // Adding two integer value.
      sum = a + b;

      // Returning summation of two values.
      return sum;
   }
}
public class Add_Test {
   public static void main(String[] args){

      // Creating an instance of Addition class
      Adding_two_Nums add = new Adding_two_Nums();

      // Calling addTwoInt() method to add two integers
      // using the instance created in the above step.
      int s = add.addTwoInt(8, 2);
      System.out.println("Sum of two "+ "integer values: "+ s);
   }
}

Output

On execution, it will produce the following output

Sum of two integer values: 10

Difference between Constructor and Method

The following table highlights the major differences between a Constructor and a Method −

Basis of Comparison
Constructor
Method
Invocation
The system invokes it implicitly.
It is called during program execution.
Inheritance
It is not possible for a subclass to inherit it.
It is anything that can be inherited by a subclass.
Uses
It is utilized in the process of initializing an object.
It is utilized for the purpose of demonstrating the operation of an object.
Return Type
There is no return type associated with it.
It is equipped with a return type.
Name
It is a common misconception that the name of the constructor does not have to match the name of the class.
It is a common misconception that the name of the method and the class cannot be the same.

Conclusion

Methods are a set of instructions that specify how a class's data can be utilised, and they are made up of one or more lines of code. Methods are analogous to a subprogram in the sense that they process data and return a value. It is possible to use the method name to invoke its execution at any point inside the body of a program.

Constructors are quite similar to methods, with the key difference being that constructors are invoked whenever an instance of an object is created. Constructors, as opposed to methods, are invoked in order to create and initialise objects that have not yet been created. In Java, constructors must be called with the same name as the name of the class in which they live, whereas methods can have any name and can be called directly either with a reference to the class or an object reference. Constructors are an exception to this rule.

raja
Updated on 28-Jul-2022 10:26:00

Advertisements