Constructor In Java
Constructor
A constructor is like a special method in Java, which is used to initialize an object of a class. It is very important for every class and if we don’t declare a constructor, the time compiler creates a default constructor of a class.
- A constructor must be a same name as a class.
- It does not have any return type.
Constructors are of two types in Java, which are,
- Default Constructor
- Parameterized Constructor
Default Constructor
Default constructor has no parameter and if we do not declare a constructor, the time compiler automatically creates a default constructor of that class.
Syntax
<class_name> () {}
Let’s see an example of default constructor, given below.
In this example, first we create a constructor in the teddy class. This is invoked, when an object is created.
Code
- public class Teddy {
- String name;
- String color;
- Teddy() {
- System.out.println("teddy is soft");
- }
- public static void main(String args[]) {
- Teddy t = new Teddy();
- }
- }
Output
If you are not creating any constructor, the compiler creates by default.
Default constructor is used to provide the default value to the object like 0, null etc. but it depends on their type.
For example
In this example, we are not creating a constructor compiler, which creates it by default and provides default value.
Code
- public class Teddy {
- String name;
- String color;
- void display() {
- System.out.println(name + "," + color);
- }
- public static void main(String args[]) {
- Teddy t = new Teddy();
- t.display();
- }
- }
Output
Parameterized constructor
A constructor, which has parameters is called a Parameterized constructor in Java.
We use Parameterized constructor, when we want to provide different values to the distinct objects.
Example of Parameterized constructor
In the example, given below, we create the constructor of teddy class, which has two parameters.
Code
- public class Teddy {
- String name;
- String color;
- Teddy(String n, String c) {
- name = n;
- color = c;
- }
- void display() {
- System.out.println("Teddy name : " + name + "," + "Teddy color : " + color);
- }
- public static void main(String args[]) {
- Teddy t = new Teddy("Sweety", "Pink");
- t.display();
- }
- }
Output
Constructor Overloading
Like methods, we can overload the constructors also, which are differentiated on the basis of their types of the parameters. In Constructor overloading, we have multiple constructors but with different parameters.
Let’s see an example, given below.
In this example, given below, we create two constructors with the different parameters.
Code
- public class Dog {
- String name;
- int age;
- Dog(String n) {
- name = n;
- }
- Dog(String n, int a) {
- name = n;
- age = a;
- }
- void display() {
- System.out.println(name + " " + age);
- }
- public static void main(String args[]) {
- Dog d1 = new Dog("tommy", 5);
- Dog d2 = new Dog("jonny", 10);
- d1.display();
- d2.display();
- }
- }
Output
In Java, what is the difference between constructor and methods?
Constructor
- We use constructor to initialize the state of an object.
- Constructor can’t return any value.
- It must have a same name as of a class.
- Compiler automatically provides default constructor.
Method
- We use method to expose the behavior of an object.
- Method must return a value.
- Method name is different from a class name.
- Compiler does not create any method by default.
Does a constructor return any value?
Yes, it returns current instance of a class and not a value.
What is constructor chaining in Java?
Constructor chaining in Java is used to call one constructor from another constructor in the same class. Hence, we can call a constructor from another constructor in Java. In constructor chaining, we use this() to call another constructor in the same class and this() must be the first line in the constructor.
Let’s see an example, given below.
Code
- public class ConstructorChain {
- ConstructorChain() {
- System.out.println("Default constructor invoked");
- }
- ConstructorChain(int x) {
- this();
- System.out.println("Single parameter constructor invoked");
- }
- ConstructorChain(int x, int y) {
- this(y);
- System.out.println("Double parameter constructor invoked");
- }
- public static void main(String a[]) {
- ConstructorChain chain = new ConstructorChain(8, 10);
- }
- }
Output
Summary
Thus, we learnt that a constructor is like a special method in Java, which is used to initialize an object of a class and also learnt about constructor overloading and chaining.