This blog post on Java’s Constructors is the continuation of our previous article, which was on Objects & Classes in Java. Well, you must know Constructor in Java is a part of the Class. Since it’s a broad topic so I wanted to discuss it separately; that’s why I came with this blog post.
What is Constructor in Java?
The constructor is a special type of method in Java that doesn’t perform any action, rather than initializing values to the variables. They do not have any return type. Whenever an instance of the class is created with a new keyword, a constructor is always called and some values are set to the variables.
Objects are assigned some memory in the memory when constructors are called.
If we don’t define any constructor explicitly then in such case default constructors are called and default values are assigned to primitive, non-primitive and wrapper variables. For example, on calling default constructor, integer gets zero (0) as the default value, boolean gets false, String gets null value, etc.
In layman terms, we can say that a constructor always constructs values to the variables, either default values or the parameterized one.
After the above discussion, we derive some rules to create a constructor. Let’s have a look at them:
Rules to create a Constructor of the Class in Java
- The name of the given class will be assigned the name of the constructor
- A constructor will not have an explicit return type
- Access modifiers are assigned to Constructor to controls the object or instance creation. Hence, a constructor can have public, protected, default and private access modifiers
- A constructor cannot be static, final, abstract, synchronized and volatile
Types of Constructor in Java
As discussed above, there are mainly two types of Constructors in Java.
- Default Constructor
- Parameterized Constructor
Default Constructor
A default constructor of a class does not have any arguments. The compiler automatically creates a default constructor when there is no constructor in a class. Thus, a default constructor sets the default values to the variables like zero to integers, null to Strings or any wrappers, false to booleans, etc.
Sample Program
package my.project; public class DefaultContructorInJava { int i; String s; //This is default constructor DefaultContructorInJava(){ } public static void main(String[] args){ DefaultContructorInJava d = new DefaultContructorInJava(); System.out.println("Default value of i: "+d.i); System.out.println("Default value of s: "+d.s); } }
Parameterized Constructor
When a constructor is explicitly defined in the class with some parameters or arguments then it is called a Parameterized constructor. It is created to assign non-zero (other than default) values to the variables.
Sample Program
package my.project; public class ParameterizedConstructorInJava { int i; String s; ParameterizedConstructorInJava(int i, String s){ this.i=i; this.s=s; System.out.println("Value of i: "+i); System.out.println("Value of s: "+s); } public static void main(String[] args){ ParameterizedConstructorInJava p = new ParameterizedConstructorInJava(2, "Avinash"); } }
Constructor Overloading in Java
When a Java class has multiple constructors with a different number of arguments then it is called Constructor overloading. Java compiler differentiates among these constructors by considering its number of arguments.
package my.project; public class ConstructorOverloadingInJava { int i; String s; ConstructorOverloadingInJava(int i, String s){ this.i=i; this.s=s; System.out.println("Value of i: "+i); System.out.println("Value of s: "+s); } ConstructorOverloadingInJava(int i, String s, boolean boo){ this.i=i; this.s=s; System.out.println("Value of i: "+i); System.out.println("Value of s: "+s); System.out.println("Value of boo: "+boo); } ConstructorOverloadingInJava(int i, String s, String s1){ this.i=i; this.s=s; System.out.println("Value of s1: "+s1); } public static void main(String[] args){ ConstructorOverloadingInJava o1 = new ConstructorOverloadingInJava(1, "a"); ConstructorOverloadingInJava o2 = new ConstructorOverloadingInJava(1, "a", true); ConstructorOverloadingInJava o3 = new ConstructorOverloadingInJava(1, "a", "b"); } }
How to copy the values of one object from another using Constructor?
package my.project; public class CopyValuesUsingConstructor { int rollNo; String name; CopyValuesUsingConstructor(int i, String s){ rollNo=i; name=s; } CopyValuesUsingConstructor(CopyValuesUsingConstructor cObj){ rollNo = cObj.rollNo; name = cObj.name; } public void displayVars(){ System.out.println("Roll No: "+rollNo); System.out.println("Name: "+name); } public static void main(String[] args) { CopyValuesUsingConstructor obj = new CopyValuesUsingConstructor(1, "Avinash"); CopyValuesUsingConstructor obj1 = new CopyValuesUsingConstructor(obj); obj.displayVars(); obj1.displayVars(); } }
Concept of Destructors in Java
Destructor is used to de-allocate (destruct) the memory assigned by constructors. There is no such explicit destructor keyword in Java, like we have Constructor. But, through some technique, we can achieve the de-allocation of memory assigned by Constructors.
Example:
package my.project; public class DestructorExample { public static void main(String[] args) { DestructorExample de = new DestructorExample(); de = null; System.gc(); System.out.println("I am in the Main Method"); } protected void finalize() { System.out.println("Object is collected by garbage"); } }
package my.project; public class NewDestructorExample { public void finalize() { System.out.println("Object is collected by garbage"); } public static void main(String[] args) { NewDestructorExample nde1 = new NewDestructorExample(); NewDestructorExample nde2 = new NewDestructorExample(); nde1 = nde2; System.gc(); System.out.println("I am in the Main Method"); } }
Miscellaneous Concepts
- A constructor doesn’t have an explicit return type, but it returns the current instance of the class.
- A constructor can perform other operations other than memory initialization, like Method calling, Assigning a value to the super constructor, Constructor chaining, Starting a thread, Object creation, etc.
- We have an explicit Constructor class in Java that resides within the reflection package. It is used to gather internal information about the constructor at runtime.
That’s all about constructors in Java. If you have any questions, feel free to ask me and do not forget to join our Facebook group for more updates.