Constructor (object-oriented Programming) - Java

Java

In Java, some of the differences between other methods and constructors are:

  • Constructors never have an explicit return type.
  • Constructors cannot be directly invoked (the keyword “new” must be used).
  • Constructors cannot be synchronized, final, abstract, native, or static.

Apart from this, a Java constructor performs the following functions in the following order:

  1. It initializes the class variables to default values. (Byte, short, int, long, float, and double variables default to their respective zero values, booleans to false, chars to the null character ('\u0000') and references of any objects to null.)
  2. It then calls the super class constructor (default constructor of super class only if no constructor is defined).
  3. It then initializes the class variables to the specified values like ex: int var = 10; or float var = 10.0f and so on.
  4. It then executes the body of the constructor.

In Java, C#, and VB .NET for reference types the constructor creates objects in a special part of memory called heap. On the other hand the value types (such as int, double etc.), are created in a sequential memory called stack. VB NET and C# allow use of new to create objects of value types. However, in those languages even use of new for value types creates objects only on stack. In C++ when constructor is invoked without new the objects are created on stack. On the other hand when objects are created using new they are created on heap which must be deleted implicitly by a destructor or explicitly by a call to operator delete.

Most languages provides a default constructor if programmer provides no constructor. However, this language provided constructor is taken away as soon as programmer provides any constructor in the class code. In C++ a default constructor is REQUIRED if an array of class objects is to be created. Other languages (Java, C#, VB .NET) have no such restriction.

In C++ copy constructor is called implicitly when class objects are returned from a method by return mechanism or when class objects are passed by value to a function. C++ provides a copy constructor if programmer provides no constructor at all. That is taken away as soon as any constructor is provided by the programmer. C++ provided copy constructor ONLY makes member-wise copy or shallow copies. For deep copies a programmer written copy constructor that makes deep copies will be required. Generally a rule of three is observed. For a class that should have a copy constructor to make deep copies, the three below must be provided. 1. Copy constructor 2. Overloading of assignment operator. 3. A destructor. The above is called rule of three in C++. If cloning of objects is not desired in C++ then copy constructor must be declared private.

public class Example { //definition of the constructor. public Example { this(1); } //overloading a constructor public Example(int input) { data = input; //This is an assignment } //declaration of instance variable(s). private int data; } //code somewhere else //instantiating an object with the above constructor Example e = new Example(42);

Read more about this topic:  Constructor (object-oriented Programming)