Open In App

Constructor Overloading with Static Block in Java

Last Updated : 07 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, Constructor is a block of codes similar to the method that is used to initialize the object’s state. A constructor is invoked at the time of object or instance creation. Each time an object is created using a new() keyword at least one constructor (it could be default constructor) is invoked to assign initial values to the data members of the same class.

Java




// A Java program to demonstrates Constructor
class Emp {
    int id;
    String name;
 
    // this would be invoked while an object
    // of that class is created.
    public Emp() { System.out.println("GeeksforGeeks"); }
}
 
// Driver Code
public class GFG {
    public static void main(String[] args)
    {
        // this would invoke default constructor.
        Emp obj = new Emp();
    }
}


Output

GeeksforGeeks

Constructor Overloading:

Like Method Overloading, we can also overload constructor. In Constructor overloading, we can create multiple constructors with different parameters, Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading.

In this example, we have created two constructors with the same class name, parameters for each constructor are different, and to access the constructor we pass arguments through the object class. As we are passing the value 10 through object class, the second constructor with int argument gets called, if no value is passed through the object class then the default constructor gets called.

Java




// Java program to demonstrates
// Constructor Overloading
class Emp {
   
    int id;
   
    // Default Constructor
    public Emp() // Constructor1
    {
        System.out.println("Default Constructor");
    }
 
    // Parameterized Constructor
    public Emp(int id)
    {
        System.out.println("Parameterized Constructor");
    }
}
 
// Driver Code
public class GFG {
   
    public static void main(String[] args)
    {
        // Accessing parameterized constructor
        Emp obj = new Emp(10);
    }
}


Output:

Parameterized Constructor

Constructor Overloading with static block

A static block in Java is used for static initializations of a class. Static block executes when the class is loaded in the memory. The code inside the static block is executed only once. Static block can also help in reducing lines of code. Let’s understand why we need Constructor Overloading with a static block in Java.  Suppose a company hired a few employees with no experience or two years of experience for a unique role developer. Since newly hired candidates are under the developer category, we can use a static block to do this.

Java




// Java program to demonstrates Constructor
// Overloading with static block
class Emp {
    int id, exp;
    String name;
    static String category;
 
    // Static block with category Developer
    static { category = "Developer"; }
   
    // Default Constructor
    public Emp()
    {
        System.out.println("-"
                           + "\t"
                           + "-"
                           + "\t"
                           + "-"
                           + "\t"
                           + "\t"
                           + "-");
    }
   
    // Parameterized Constructor with two arguments
    public Emp(int id, String name)
    {
        System.out.println(id + "\t" + name + "\t"
                           + category + "\t" + exp);
    }
   
    // Parameterized Constructor with three arguments
    public Emp(int id, String name, int exp)
    {
        System.out.println(id + "\t" + name + "\t"
                           + category + "\t" + exp);
    }
}
 
// Driver Code
public class GFG {
   
    public static void main(String[] args)
    {
        System.out.println("Id"
                           + "\t"
                           + "Name"
                           + "\t"
                           + "Category"
                           + "\t"
                           + "Exp");
       
        // Passing values to parameterized constructor with
        // two arguments
        Emp obj1 = new Emp(1863, "Kumar");
       
        // Passing values to parameterized constructor with
        // three arguments
        Emp obj2 = new Emp(1864, "ravi", 2);
       
        // Calling Default COnstructor
        Emp obj3 = new Emp();
    }
}


Output

Id    Name    Category    Exp
1863    Kumar    Developer    0
1864    ravi    Developer    2
-    -    -        -

In the above example, inside the parameterized constructor with two arguments we are passing the only id, name from the object, In the output, we can also see category and exp is also printed, this is because we have created a static block for the category Developer and java automatically assigns 0 if no value is passed to it. Inside the parameterized constructor with three arguments, we are only passing id, name, exp but the category is also printed in output this is because the category is in static block.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads