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
class Emp {
int id;
String name;
public Emp() { System.out.println( "GeeksforGeeks" ); }
}
public class GFG {
public static void main(String[] args)
{
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
class Emp {
int id;
public Emp()
{
System.out.println( "Default Constructor" );
}
public Emp( int id)
{
System.out.println( "Parameterized Constructor" );
}
}
public class GFG {
public static void main(String[] args)
{
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
class Emp {
int id, exp;
String name;
static String category;
static { category = "Developer" ; }
public Emp()
{
System.out.println( "-"
+ "\t"
+ "-"
+ "\t"
+ "-"
+ "\t"
+ "\t"
+ "-" );
}
public Emp( int id, String name)
{
System.out.println(id + "\t" + name + "\t"
+ category + "\t" + exp);
}
public Emp( int id, String name, int exp)
{
System.out.println(id + "\t" + name + "\t"
+ category + "\t" + exp);
}
}
public class GFG {
public static void main(String[] args)
{
System.out.println( "Id"
+ "\t"
+ "Name"
+ "\t"
+ "Category"
+ "\t"
+ "Exp" );
Emp obj1 = new Emp( 1863 , "Kumar" );
Emp obj2 = new Emp( 1864 , "ravi" , 2 );
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Sep, 2021
Like Article
Save Article