articles

Home / DeveloperSection / Articles / Introduction of Constructor

Introduction of Constructor

Hemant Patel 1997 20-Jan-2017

Constructor:-  It is a special type of method that JVM executes during construction of each object.

We can define this method in our circle class to supply value (radius value) to the JVM.

So that Jvm can initialize radius field by my value.

                            class Circle
{
int radius;
Circle(int r) //Constructor
{
System.out.println(“Constructor executed”);
radius=r;
}
public static void main(string [] args)
{
Circle C1=new Circle(10);
Circle C2=new Circle(20);
Circle C3=new Circle(30);
System.out.println(“C1.radius”);
System.out.println(“C2.radius”);
System.out.println(“C3.radius”);
}
}
 Key points about constructor:-

1.Name of the constructor must be name of the class.

2.It can not have any return type.

3. JVM always executes constructor during construction of an object.It means without constructor an object will not be constructed.So,every class must have at least one constructor.Compiler has responsibility to ensure it.

4.During compilation compiler checks existence of atleast one constructor in the source code of the class.

If source code does not have any constructor then compiler insert a non-parameterized constructor in the byte code, i.e. in the class file.

This constructor is known as Default constructor.

5.As we know constructor is a method  so it can be defined to perform any task.But it is better to defined it for the initialization of object.

6.It can not be called explicitly.Because JVM will execute it during construction of an object.

7.If you will give return type of constructor then both compile and JVM will not treat it as the constructor. It will be treated as a method.

class AA
{
   void AA()
   {
   System.out.println(“C1.radius”);
   }
   public static void main(string [] args)
    {
    new AA();
    }
}

Types of constructor:-

Constructor can be of following three types:-

1.    Default constructor

2.    Parameterized constructor

3.    Copy constructor

1.    Default constructor:- The constructor i.e. inserted by compiler in the class file, is known as default constructor. Compiler does it when source code of the class file does not have any constructor. If you have defined this constructor in the source code then it is called non-parameterized constructor. This constructor is not be used to initialize object because each object will be initialized by same value.

2.    Parameterized constructor:- A constructor which access parameter during construction of the object is known as parameterized constructor.It should be defined by programmer. Object initialization should also be done by this constructor.

                           class Cirlce
{
int radius;
Circle();// default constructor
{
System.out.println(‘”Default constructor executed”);
radius =20;
}
Circle(int r) //parameterized constructor
{
System.out.println(“parameterized constructor executed”);
}
public static void main(string [] args)
{
Circle c1=new Circle(19);
Circle c2=new Circle();
Circle c3=new Circle(29);
System.out.println(c1.radius);
System.out.println(c2.radius);
System.out.println(c3.radius);
}
}

3. Copy constructor:- A constructor which accepts reference of an existing object as a parameter and copy it values of existing object fields into the new object is know as ‘Copy Constructor’. It means copy constructor is used to create duplicate objects.

Copy constructor is also parameterized constructor in java programming language.

                                  class Employee
{
int eid;
string name;
int salary;
Employee()
{  }
Employee(int s1,String s2,int s3) // parameterized
{
eid=s1;
name=s2;
salary=s3;
}
Employee(Employee old)//copy constructor
{
this.eid=old.eid;
this.name=old.name;
this.salary=old.salary;
}
  void showDetails()
{
System.out.println(eid + “ “ +name+ “ ” +salary);
}
public static void main(string [] args)
{
Employee e1= new Employee(101,”James”,20000);
Employee e2= new Employee(e1);
Employee e3= new Employee(e1);
E1.showDetails();
E2.showDetails();
E3.showDetails();
}
}

Updated 30-Jan-2019
Exploring the online world is my passion. have experience of #content writing #SEO #Digital Marketing #On-Page #Lead Generation #Content Analyst #Marketing Analyst... I could never stop at one point, continuously wanted to acquire more and more skills. My work for any organization will be full of passion and hard work.

Leave Comment

Comments

Liked By