Difference between the Constructors and Methods

Java is a pure OOPS concept based programming language. Hence in Java, all the variables, data and the statements must be present in classes. These classes consist of both constructors and methods. Methods and Constructors are different from each other in a lot of ways.

Constructors:
Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation. Each time an object is created using new() keyword at least one constructor (it could be default constructor) is invoked to assign initial values to the data members of the same class.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate constructor
  
import java.io.*;
  
class Geek {
    int num;
    String name;
  
    // This would be invoked while an object
    // of that class created.
    Geek()
    {
        System.out.println("Constructor called");
    }
}
  
class GFG {
    public static void main(String[] args)
    {
        // this would invoke default constructor.
        Geek geek1 = new Geek();
  
        // Default constructor provides the default
        // values to the object like 0, null
        System.out.println(geek1.name);
        System.out.println(geek1.num);
    }
}

chevron_right


Output:

Constructor called
null
0

Methods:
A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++, and Python.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate methods
  
import java.io.*;
  
class Addition {
  
    int sum = 0;
  
    public int addTwoInt(int a, int b)
    {
  
        // Adding two integer value.
        sum = a + b;
  
        // Returning summation of two values.
        return sum;
    }
}
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating an instance of Addition class
        Addition add = new Addition();
  
        // Calling addTwoInt() method
        // to add two integer
        // using instance created
        // in above step.
        int s = add.addTwoInt(1, 2);
  
        System.out.println("Sum of two "
                           + "integer values: "
                           + s);
    }
}

chevron_right


Output:

Sum of two integer values: 3

Differences between Constructors and Methods:

Constructors Methods
A Constructor is a block of code that initializes a newly created object. A Method is a collection of statements which returns a value upon its execution.
A Constructor can be used to initialize an object. A Method consists of Java code to be executed.
A Constructor is invoked implicitly by the system. A Method is invoked by the programmer.
A Constructor is invoked when a object is created using the keyword new. A Method is invoked through method calls.
A Constructor doesn’t have a return type. A Method must have a return type.
A Constructor initializes a object that doesn’t exist. A Method does operations on an already created object.
A Constructor’s name must be same as the name of the class. A Method’s name can be anything.
A class can have many Constructors but must not have the same parameters. A class can have many methods but must not have the same parameters.
A Constructor cannot be inherited by subclasses. A Method can be inherited by subclasses.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai