Verification: a143cc29221c9be0

Php class constructor same name as class

What is Constructor in Java?

CONSTRUCTOR is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object. It can be used to initialize the objects to desired values or default values at the time of object creation. It is not mandatory for the coder to write a constructor for a class.

Rules for creating a Java Constructor

  1. It has the same name as the class
  2. It should not return a value not even void

Example 1: Create your First Constructor in Java

Step 1) Type the following constructor program in Java editor.

class Demo{
      int  value1;
      int  value2;
      Demo(){
         value1 = 10;
         value2 = 20;
         System.out.println("Inside Constructor");
     }
 
     public void display(){
        System.out.println("Value1 === "+value1);
        System.out.println("Value2 === "+value2);
    }
 
   public static void main(String args[]){
       Demo d1 = new Demo();
      d1.display();
  }
}

Step 2) Save , Run & Compile the constructor program in Java and observe the output.

Output:

Inside Constructor
Value1 === 10
Value2 === 20

Constructor Overloading in Java

Java Constructor overloading is a technique in which a class can have any number of constructors that differ in parameter list. The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.

Examples of valid  constructors for class Account are

Account(int a);
Account (int a,int b);
Account (String a,int b);

Example 2: To understand Constructor Overloading in Java

Step 1) Type the code in the editor.

class Demo{
      int  value1;
      int  value2;
      /*Demo(){
       value1 = 10;
       value2 = 20;
       System.out.println("Inside 1st Constructor");
     }*/
     Demo(int a){
      value1 = a;
      System.out.println("Inside 2nd Constructor");
    }
    Demo(int a,int b){
    value1 = a;
    value2 = b;
    System.out.println("Inside 3rd Constructor");
   }
   public void display(){
      System.out.println("Value1 === "+value1);
      System.out.println("Value2 === "+value2);
  }
  public static void main(String args[]){
    Demo d1 = new Demo();
    Demo d2 = new Demo(30);
    Demo d3 = new Demo(30,40);
    d1.display();
    d2.display();
    d3.display();
 }
}

Step 2) Save, Compile & Run the Code.

Step 3) Error = ?. Try and debug the error before proceeding to next step of Java constructor overloading

Step 4) Every class has a default Constructor in Java. Default overloaded constructor Java for class Demo is Demo(). In case you do not provide this constructor the compiler creates it for you and initializes the variables to default values. You may choose to override this default constructor and initialize variables to your desired values as shown in Example 1.

But if you specify a parametrized constructor like Demo(int a), and want to use the default constructor Java Demo(), it is mandatory for you to specify it.

In other words, in case your overloading constructor in Java is overridden, and you want to use the default constructor Java, its need to be specified.

Step 5) Uncomment line # 4-8. Save, Compile & Run the code.