Constructor

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.

Advertisements

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.

Advertisements

Related Terms

Latest DevOps Terms

Related Reading

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…