Constructor in Java: A complete study.
Constructor is a block of code that initializes the newly created object. A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type. In short constructor and method are different(More on this at the end of this guide). People often refer constructor as special type of method in Java.
Constructor is a block of code that initializes the newly created object. A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type. In short constructor and method are different(More on this at the end of this guide). People often refer constructor as special type of method in Java.
Constructor has same name as the class and looks like this in a java code.
public class MyClass{ //This is the constructor MyClass(){ } .. }
Note that the constructor name matches with the class name and it doesn’t have a return type.
How does a constructor work
To understand the working of constructor, lets take an example. lets say we have a class
When we create the object of
MyClass
.When we create the object of
MyClass
like this:MyClass obj = new MyClass()
The new keyword here creates the object of class
MyClass
and invokes the constructor to initialize this newly created object.
You may get a little lost here as I have not shown you any initialization example, lets have a look at the code below:
A simple constructor program in java
Here we have created an object
obj
of class Hello
and then we displayed the instance variable name
of the object. As you can see that the output is BeginnersBook.com
which is what we have passed to the name
during initialization in constructor. This shows that when we created the object obj
the constructor got invoked. In this example we have used this keyword, which refers to the current object, object obj
in this example. We will cover this keyword in detail in the next tutorial.public class Hello { String name; //Constructor Hello(){ this.name = "BeginnersBook.com"; } public static void main(String[] args) { Hello obj = new Hello(); System.out.println(obj.name); } }
Output:
BeginnersBook.com
Types of Constructors
There are three types of constructors: Default, No-arg constructor and Parameterized.
Default constructor
If you do not implement any constructor in your class, Java compiler inserts a default constructor into your code on your behalf. This constructor is known as default constructor. You would not find it in your source code(the java file) as it would be inserted into the code during compilation and exists in .class file. This process is shown in the diagram below:
If you implement any constructor then you no longer receive a default constructor from Java compiler.
no-arg constructor:
Constructor with no arguments is known as no-arg constructor. The signature is same as default constructor, however body can have any code unlike default constructor where the body of the constructor is empty.
Although you may see some people claim that that default and no-arg constructor is same but in fact they are not, even if you write public Demo() { }in your classDemo
it cannot be called default constructor since you have written the code of it.
Example: no-arg constructor
class Demo { public Demo() { System.out.println("This is a no argument constructor"); } public static void main(String args[]) { new Demo(); } }
Output:
This is a no argument constructor
This is a no argument constructor
Parameterized constructor
Constructor with arguments(or you can say parameters) is known as Parameterized constructor.
Example: parameterized constructor
In this example we have a parameterized constructor with two parameters
id
and name
. While creating the objects obj1
and obj2
I have passed two arguments so that this constructor gets invoked after creation of obj1 and obj2.public class Employee { int empId; String empName; //parameterized constructor with two parameters Employee(int id, String name){ this.empId = id; this.empName = name; } void info(){ System.out.println("Id: "+empId+" Name: "+empName); } public static void main(String args[]){ Employee obj1 = new Employee(10245,"Chaitanya"); Employee obj2 = new Employee(92232,"Negan"); obj1.info(); obj2.info(); } }
Output:
Id: 10245 Name: Chaitanya Id: 92232 Name: Negan
Example2: parameterized constructor
In this example, we have two constructors, a default constructor and a parameterized constructor. When we do not pass any parameter while creating the object using new keyword then default constructor is invoked, however when you pass a parameter then parameterized constructor that matches with the passed parameters list gets invoked.
class Example2 { private int var; //default constructor public Example2() { this.var = 10; } //parameterized constructor public Example2(int num) { this.var = num; } public int getValue() { return var; } public static void main(String args[]) { Example2 obj = new Example2(); Example2 obj2 = new Example2(100); System.out.println("var is: "+obj.getValue()); System.out.println("var is: "+obj2.getValue()); } }
Output:
var is: 10
Constructor Overloading
Constructor overloading is a concept of having more than one constructor with different parameters list, in such a way so that each constructor performs a different task.
Refer constructor overloading with examplefor more details with example.
var is: 100
Difference between Constructor and Method
I know I should have mentioned it at the beginning of this guide but I wanted to cover everything in a flow. Hope you don’t mind :)
- The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code.
- Constructors cannot be abstract, final, static and synchronised while methods can be.
- Constructors do not have return types while methods do
No comments:
Post a Comment