Constructor in JAVA.

Last Updated on by ICT Byte

Constructor in JAVA: Introduction

  • special member method
  • it is called automatically (implicitly) when object is created
  • object is created to place own values, i.e not placing default values

Constructor is special member method which is called automatically when object is created.

What is the purpose of a constructor?

  • To initialize an object
  • This process in called initialization
  • Initializing is the process where user defined values are assigned at the time of memory allocation.

Syntax of Constructor

className()
{




}

Advantage of constructors in Java


If we write any Java program and we use concept of constructor, it

  • eliminates the palcement of default value
  • eliminates calling the normal method implicitly

Characteristics / Properties of Constructor

  • The name of the constructor must be the same as the name of the class
  • When an object is created, a constructor is called implicitly
  • The constructor should not return any value. This is true even return type is void. This means the constructor has no return type
  • There is no inheritance in the constructor
  • A constructor should not be static.
  • Constructors are not virtual
  • Constructors can be overloaded as they are member functions.

Types of Constructors

On the basis of creating an object, Java Constructors are divided into two.

  • Default/parameter less/zero argument/no argument constructor
  •   Parameterized constructor

Default Constructor

  • it never takes any parameters

Syntax


class NameofClass
{

classname()
{
//Statements;
}

}

Purpose of default constructor

  • to place same values in multiple objects of same class
  • provides default values to the objects
  • provides values depending on type
  • example for integer 0, for string null

Parameterized Constructor

  • takes parameter
  • contains list of variables in signatures

Syntax


class NameOfClass
{
NameofClass(list of parameters)
{

}
…..
}

Syntax for calling parameterized Constructor


new NameofClass(val1, val2, …)

Purpose of Parameterized constructor

  • create multiple objects of same class for different values

Overloaded Constructor

  • If the name of constructor is same but its signature are different, constructor is said to be overloaded.

What is signature?


Signature represents

  • number of parameters
  • parameter types
  • order of the parameter

Example
Test  t1= new Test(10, 20); –>1
Test  t2= new Test(100); –>2
Test  t3= new Test(10.5f, 20.5f); –>3
Test  t4= new Test(10.5f, 20); –>4
Test  t5= new Test(10, 20.5f); –>5
Here Test(—) is known as Overloaded Constructor.

Object Parameterized Constructor

  • it always takes object as a parameter
  • it aims to copy the content of object to another object (both objects should belong to same type)
  • this exactly resembles the copy constructor of C++

Example


class TestExample
       {
                int a,b;
                TestExample() // Default Constructor
                {
                                System.out.println("i am Default Constructor:");
                                x=1;
                                y=2;
                                System.out.println("The value of x="+x);
                                System.out.println("The value of y="+y);
                }

                TestExample(int a,int b)// Double parameterized constructor
                {
                                System.out.println("i am  Double Parameterized Constructor:");
                                x=a;
                                y=b;
                                System.out.println("The value of x="+x);
                                System.out.println("The value of by="+y);
                }

                TestExample(int a)// Single parameterized constructor
                {
                                System.out.println("i am  Single Parameterized Constructor:");
                                x=a;
                                y=a;
                                System.out.println("The value of x="+x);
                                System.out.println("The value of y="+y);
                }

                TestExample(TestExample a)// Object parameterized constructor
                {
                                System.out.println("i am  Object Parameterized Constructor:");
                                x=a.x;
                                y=a.y;
                                System.out.println("The value of x="+x);
                                System.out.println("The value of y="+y);
                }
       }

    class TestExample
       {
                public static void main(String[] args)
                {
                                TestExample t1= new TestExample();
                                TestExaple t2= new TestExample(10,20);
                                TestExample t3= new TestExample(100);
                                TestExample t4= new TestExample(t3);
                }
      }

More From Author

What is encapsulation? Learn encapsulation in JAVA

Samsung Galaxy A52 Price and Specifications