In this post we will explain type of constructors in java and what is the difference between constructors and methods.
What is a constructor
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.
Note that the constructor name matches with the class name and it doesn’t have a return type.
Constructor has same name as the class and looks like this in a java code.
public class MyClass { //This is the constructor MyClass(){ } .. }
How does a constructor work
To understand the working of constructor, lets take an example. lets say we have a class 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.
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.
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 class Demo
it cannot be called default constructor since you have written the code of it.
class Demo { public Demo() { System.out.println("This is a no argument constructor"); } public static void main(String args[]) { new Demo(); } }
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; } }
This are all type of constructors in java, use them on the correct time and enjoy coding.
Difference between Constructor and Method
- 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 synchronized while methods can be.
- Constructors do not have return types while methods do.