Constructor vs Method in Java

In this post, we will learn the difference between Constructor and Method in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.

Difference between Constructor and Method in Java

Constructor Method
A constructor is a special type of method that is used to initialize the state of an object. A method is a block of code that performs a specific task.
The name of the constructor must be the same as the name of the class. A method can have any name, it is not tied to the class name.
It does not have a return type, not even void. It has a return type. If no value is returned, then the method has a 'void' return type.
A constructor is automatically called when an object is created. Methods need to be called explicitly.
In Java, if no constructor is defined in a class, the compiler automatically provides a default constructor. There is no default method provided by the compiler.
It is used to allocate memory to the object. It does not allocate memory but occupies the memory of an object.
Constructors can not be inherited, though a derived class can call the base class constructor. Methods are inherited from the subclasses and can be overridden.
There is no concept of abstract or final constructors. Methods can be abstract, final, or synchronized.

Example

Let's create an example to illustrate the difference between constructors and methods in Java, along with the output.
public class ConstructorVsMethodExample {
    private String name;

    // Constructor
    public ConstructorVsMethodExample(String name) {
        this.name = name;
        System.out.println("Constructor called with name: " + name);
    }

    // Method
    public void printMessage(String message) {
        System.out.println("Method called with message: " + message);
    }

    public static void main(String[] args) {
        // Creating an object using the constructor
        ConstructorVsMethodExample obj1 = new ConstructorVsMethodExample("Object1");

        // Calling the method on the object
        obj1.printMessage("Hello from Method");

        // Creating another object using the constructor
        ConstructorVsMethodExample obj2 = new ConstructorVsMethodExample("Object2");

        // Calling the method on the second object
        obj2.printMessage("Greetings from Method");
    }
}

Output:

Constructor called with name: Object1
Method called with message: Hello from Method
Constructor called with name: Object2
Method called with message: Greetings from Method

Explanation: 

We have a class ConstructorVsMethodExample with a constructor and a method. The constructor is a special method that is called automatically when an object of the class is created using the new keyword.

In this example, we have a parameterized constructor that takes a name argument and prints a message indicating that the constructor is called with the given name. 

The printMessage method is a regular method that takes a message argument and prints a message indicating that the method is called with the given message. 

In the main method, we create two objects obj1 and obj2 using the constructor. When each object is created, the constructor is called with the provided name. 

We then call the printMessage method on both objects. Each time the method is called, it prints the message passed as an argument. 

The output shows that the constructor is called when the objects are created, and the method is called when we explicitly invoke it on the objects. The constructor is used to initialize the object's state during its creation, while methods are used to perform specific actions or operations on the objects.

References

Related Posts



Comments