Why Trust Techopedia

What Does Constructor Mean?

A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.


A constructor is like an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values. However, although it resembles it, a constructor is not a proper method since it doesn’t have a return type. Instead of performing a task by executing code, the constructor initializes the object, and it cannot be static, final, abstract, and synchronized.

Techopedia Explains Constructor

Constructors are not called explicitly and are invoked only once during their lifetime. In the case of a hierarchy of classes where a derived class inherits from a parent class, the execution sequence of the constructor is a call to the constructor of the parent class first and then that of the derived class. Constructors cannot be inherited.

Users do not need to write constructors for every class. A constructor can be declared using any of the access modifiers. It is mandatory to have a constructor with the right access modifier.

However, the compiler supplies a default if an access modifier is not defined in the class and a constructor is not declared. This default constructor cannot be found inside the source code since it’s found in the .class file. Its behavior depends on the language.

Although it’s often mistaken for a default constructor, in Java constructors with no arguments also exist and are called “no-arg constructors.” While they share the signature with the default constructor, the body of no-arg constructors isn’t empty and they can have any code. Constructors with arguments, instead, are known as “parameterized constructors.”

If a constructor is declared as private, the class cannot be created or derived and hence cannot be instantiated. Such a constructor, however, can be overloaded with different sets of parameters.

The following is recommended in constructor design:

  • Logic involving specific operations that need to be executed at a particular event in an application – such as opening a database connection – should not be written in a constructor.

  • When using derived class constructors, the parent class constructor should be passed the correct parameters.

  • Better code maintainability comes from having the initialization and other related logic in one main constructor and cross-calling this constructor from other overloaded constructors.

  • Because a constructor cannot return a value to the calling code, it is a good practice to throw an exception when a failure is encountered.

Constructor chaining occurs whenever a base class is extended by a child. The parent class is, therefore, invoked first every time an object of that child class is created.


Related Terms

Margaret Rouse

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…