Web
Analytics
Constructor Calling in Constructor function in JAVA | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Constructor Calling in Constructor function in JAVA

Basic Idea of Constructor

  • Constructor in java is a special type of method that is used to initialize the object.
  • Java constructor is invoked at the time of object creation. Ex Test tobj=new Test();

Rules of Java Constructor

  • Constructor name must be same as its class name
  • Constructor must have no explicit return type

Basic Types of java constructors

  • Default constructor (no-arg constructor)
  • Parameterized constructor

Constructor Calling through Constructor 

  • Inside the constructors constructor calling must be the first statement of the contructor other wise the compiler will raise compilation error.
  • The above  rule applicable only for constructors.
public class Test {
public Test()
{
this(10);
System.out.println("0 Argument Constructor");

}
public Test(int a)
{
this(10,20);
System.out.println("1 Argument Constructor");
}
public Test(int a,int b)
{
System.out.println("2 Argument Constructor");
}
}

 

public class ConstructorCalling {

public static void main(String[] args) {
// TODO Auto-generated method stub
Test tobj=new Test();
}

}

capture

 Download Source Code