What is the difference between a Method and a Constructor

Let us look at what a constructor and a method does and then summarize with key differences between constructor and method.

Constructors and Methods are core to a Class in Object Oriented Programming. Although a constructor and a method seem to look alike, these structures serve different purposes in functionality.

Let us look at these structures and then summarize with key differences between constructor and method.

What is a Method?

a method is a regular function that defines a behavior of the class. we can create a method with a unique name and a return type. if the method doesn’t return any value, it has a void return type. It is called explicitly when a particular behavior is needed to be executed, within the class or by an external component.

A method can be public, private or protected depending on who can access it. It can be called multiple times as required.

we can write multiple versions of a method using method overloading, by varying the number of parameters or the data types of the parameters. we can also define static methods, which we can call without creating an object of the class.

A Method –

  • Is used to expose the behavior of an object
  • Must have a return type
  • Is invoked explicitly
  • Is not provided by the compiler in any case
  • Name may or may not be same as class name

What is a Constructor?

a constructor is a special type of method in a class. it is automatically called when an object is created for a class using a new keyword or when an object is defined. it is used to initialize object variables and allocate necessary resources.

It always has the same name as the class and doesn’t have a return type, since it doesn’t need to return anything. It is called only once per object creation. If a class does not contain any explicit constructor defined, the compiler creates and calls a default constructor for the class.

We can write as many constructor definitions for a class as needed. Constructors also support overloading feature, where we can have multiple constructors with varying parameter types or the number of parameters.

We can also define a static constructor, which will be called in some point of program execution when the first static member of the class is called. It is used when creating or initializing static members of the class, such as a HTTP Client.

A Constructor –

  • Is used to initialize the state of an object
  • Is invoked implicitly
  • Must not have a return type
  • Compiler provides a default constructor if you don’t have any constructor in a class
  • Name must be same as the class name

Example of a class with a Constructor and Method definitions –

public class SomeComponent 
{
    private int _someComponentVariable;
    private readonly int _someExternalParameter;

    private static int _someStaticMember;

    public SomeComponent(int someParameter) {
        _someComponentVariable = 5;
        _someExternalParameter = someParameter;
    }

    static SomeComponent() {
        _someStaticMember = 10;
    }

    public void SomeBehavior(int someParameter) {
        LogInformation(someParameter);
    }

    public void SomeBehavior(string someParameter, int someOtherParameter) {
        // do something here with the new method variant
    }
}

Conclusion – Difference between Constructors and Methods

MethodConstructor
Purposeregular function that defines a behavior of the classa special function that is used to initialize class variables and allocate resources
Naminghas a unique name depending on the behavior it definesthe name of the constructor should be same as the class name
Return Typehas an access specifier for abstraction and a return typehas an access specifier for abstraction but no return type
Invocationis invoked explicitly by other members of the class or external components as required, and can be called as many times as requiredcalled automatically by the constructor when an object is created with the new keyword and is called only once
Supportsupports overloading where we can write multiple variations of the same method with varying signaturesupports overloading where we can write multiple variants of the same constructor with varying signature

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *