ALERT

[WEBINAR] See the Whole Story: The Case for a Visualization Platform

Constructor

Definition - What does Constructor mean?

A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is 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.

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.

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. 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.

          Connect with us

          Techopedia on Linkedin
          Techopedia on Linkedin
          Tweat cdn.techopedia.com
          "Techopedia" on Twitter


          '@Techopedia'
          Sign up for Techopedia's Free Newsletter!

          Email Newsletter

          Join thousands of others with our weekly newsletter

          Resources
          Free Whitepaper: The Path to Hybrid Cloud
          Free Whitepaper: The Path to Hybrid Cloud:
          The Path to Hybrid Cloud: Intelligent Bursting To Amazon Web Services & Microsoft Azure
          Free E-Book: Public Cloud Guide
          Free E-Book: Public Cloud Guide:
          This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
          Free Tool: Virtual Health Monitor
          Free Tool: Virtual Health Monitor:
          Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
          Free 30 Day Trial – Turbonomic
          Free 30 Day Trial – Turbonomic:
          Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.