Java | Constructors

06 min read

Constructors:

Since all the instance variables of all the objects have to be initialized, it will be convenient, if this initialization is done at the time, the object is first created. This automatic initialization is performed through the use of a constructor.

Constructor:

  • Initializes an object immediately upon creation.
  • Has the same name as the class in which it resides & syntactically similar to a method.
  • Once defined, it is automatically called immediately after the object is created, before the 'new' operator completes.
  • The constructor should not have any return type (not even void) as the implicit return type of a class constructor is the class type itself.
  • If a constructor is not defined for the class explicitly, Java creates a default constructor for the class, which initializes all the instance variables to their default values (0, false, null for numeric, boolean and reference variables respectively).

Thus, in the previous example, when the objects are being created using 'new', default constructor supplied by Java, is invoked. This makes RAM = 0, hdisk = 0, freq = 0, & make=null. When the getdata( ) method is invoked, later on, it changes the values of these instance variables.

Let us modify the previous program by including a constructor for assigning values to the instance variables

Instead of getdata( ) method :

Computer( )
{
RAM = 2;  // It is a constructor as it takes the name of the class. It need not be called explicitly. Once the object is created, it will be automatically called.
hdisk = 200;                
freq = I.2e06;          
make = "IBM";
}
Computer node1 = new Computer( ); ---> calls the constructor Computer( ) automatically ( ) method is defined for initializing, it must be explicitly invoked, after creating the object.
node1.getdata( ); ---> only then, the method is invoked.

 

Parameterized Constructor:

The constructor defined above always initializes all the objects with the same values mentioned inside of it. So, node1 will also have {2, 200, 1.2e06, "IBM"}. node2 will also have {2, 200, 1.2e06, "IBM").

To supply the values which the user wants, a parameterized constructor can be written, instead of the default constructor. (Or, Input values can be read from the user within the default constructor and assigned to the instance variables).

Computer(int r, int h, float f, String s)
{
RAM =r;
hdisk = h;
freq = f;
make = s;
} ---> such a parameterized constructor will be invoked only when the object is created as follows:
Computer nodel = new Computer(2, 200, 1.2e06, "IBM");
(or)
Computer nodel = new Cornputer(r, h, f, s); ---> Where r, h, f, & s variables are assigned with some constant values or obtained as input from the user.
Once the parameterized constructor is defined, no object can be created by simply writing a statement like the one below.
Computer node3 = new Computer( ); X  ----> This will cause an error, as there is no matching constructor without any parameters (default constructor).

It is important to remember that the default constructor will be supplied by Java only if there is no constructor defined in the class. Once the user starts to define any constructor, he must also take care of defining the default constructor by himself, to handle the situation mentioned above.

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error