Constructor Overloading with Static Block in Java
Last Updated :
07 Sep, 2021
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();
}
}
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();
}
}
OutputId 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.
Similar Reads
Constructor Overloading in Java Java supports Constructor Overloading in addition to overloading methods. In Java, overloaded constructor is called based on the parameters specified when a new is executed. When do we need Constructor Overloading? Sometimes there is a need of initializing an object in different ways. This can be do
5 min read
What is Class Loading and Static Blocks in Java? Class Loading is the process of storing the class-specific information in the memory. Class-specific information means, information about the class members, i.e., variables and methods. It is just like that before firing a bullet, first, we need to load the bullet into the pistol. Similarly, to use
3 min read
Constructor newInstance() method in Java with Examples The newInstance() method of a Constructor class is used to create and initialize a new instance of this constructor, with the initialization parameters passed as parameter to this method. Each parameter is unwrapped to match primitive formal parameters, and both primitive and reference parameters ar
3 min read
Why a Constructor can not be final, static or abstract in Java? Prerequisite: Inheritance in Java Constructor in java is a special type of method which is different from normal java methods/ordinary methods. Constructors are used to initialize an object. Automatically a constructor is called when an object of a class is created. It is syntactically similar to a
6 min read
Static Method in Java With Examples In Java, the static keyword is used to create methods that belongs to the class rather than any specific instance of the class. Any method that uses the static keyword is referred to as a static method.Features of Static Method:A static method in Java is associated with the class, not with any objec
3 min read