Constructor

In the previous lesson, you were already introduced to member methods or functions. These functions, also known as an object of any particular class contains instant variables. If operations are to be performed on them, the instant variables must be initialized.

Now let us move to the concept on Constructors.

A constructor is a member method that has been declared with a name that is absolutely identical to the name of the class in which it is initialized.

Kinds of Constructors:

Default Constructor:

A constructor that initializes the values of the member variables in Java is known as Default Constructor.

Such constructors need not to be provoked because they are called by default while creating an object.

There are two ways of creating an object on the basis of where the values are supplied:

  • By Compiler itself:
class defconst

{

   int x,y;              // Instant or Member Variables.

   defonst()      /* This constructor will be called by default and will initialize member variables. */

    {

       x=10;

        y=20;

     }

   void result()           // Printing Result.

    {

          System.out.println(x+"   "+y);

       }

}

When the above program is executed, the comconst() is automatically called and the values of the instant values are initialized and the same is displayed when result() is called.

  • By the user or programmers:
class defconst

{

int x,y;    // Instant or Member Variables.

defconst()         /* This constructor will be called by default and will initialize member variables. */ { x=0;

y=0;

} void result()     // Printing Result.

{

System.out.println(x+"   "+y);

}

public static void main()

{

defconst obj=new defconst();

obj.display();

}

}

When the above program is executed, the comconst() is automatically called and the values of the instant values are initialized to zeroes and the same is displayed when obj.display() is executed.

Parameterized Constructor:

Initialization of instant variables by passing values to the constructors as arguments, generally known as a parameter list. Thus these variables are initialized when they are being created. Such a constructor is called Parameterized Constructor.

  • Constructor by the compiler:
class paraconst
{

int x,y; // Instant or Member Variables.

paraconst(int m, int n)      /This constructor will be called by default  and will initialize member variables to the parameterized values/

{

x=m;

y=n;

} void result()     // Printing Result.

{

System.out.println(x+"   "+y);

}

}

Here the instant values are initialized by the values that are passed as parameters when paraconst() is invoked.

  • Constructor by the compiler:
class paraconst

{

int x,y;        // Instant or Member Variables.

paraconst(int m, int n)      /* When called, this constructor will initialize the variables to the parameterized values  */

{

x=m;

y=n;

}

void result()     // Printing Result. {

System.out.println(x+"   "+y);

}

public static void main()

{

paraconst obj=new paraconst(10,20);   // Values passed as parameters obj.display();

}

}

The values that are passed as parameters in the main function are caught and accepted to initialize the instant variables.

Copy Constructors:

Copy Constructors are defined as the constructors that are used to copy the initial values of the global variables of one member method to the global variables of another method.

Constructor