A Constructor in a special method that is used to initialize the object.
Constructor name should be same as class name. Constructors does not have any return type.
1 2 3 4 5 6 | public class Student(){ //constructor Student(){ } } |
While creating object of above class we use
1 | Student student=new Studet() |
Here new is used to create object and after then the constructor Student() is get called it provide initial value to object
When an object is created using new operator then to initialize the object constructor is called immediately.
A class can contain more than one constructor. If a class does The constructor the constructor, the system provides the default constructor which is known as system default constructor resides.
The default constructor automatically initialize all instance variable to zero
1 Characteristics of Constructor
- The constructor has the same name as the class in which it resides.
- The constructor is automatically called immediately after the object of that class created in which it resides.
- Constructor neither returns value nor void.
- Access specifiers public protected,default and private can be used with constructors.
- The constructor can be
overloaded. - A constructor can also call another constructor.
- Default constructor automatically initialize all instance variable to zero
- Constructors can not be abstract, final ,native synchronized or static.
2 Type of Constructors
In Java Constructors are categorized in three types
- Default (No Argument) Constructor
- Parameterized Constructor
Default (No Argument) Constructor
A default constructor in no argument constructor it initialize object value to initial value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Student { private long rollNo; private String name; Student()//student default constructor { System.out.println("Inside No Argument Constructor"); } } public class StudnetTest { public static void main(String arg[]) { Student student1 = new Student();// called no argument constructor } } |
In above program no argument constructor in provided student() on executing above program it will show following result
Result
1 | Inside No Argument Constructor |
If a java programmer has written a program and didn’t provided any constructor in program in that case during compile time compiler will include a no argument constructor.
Compiler will only include this constructor it there is no constructor available in program.
If there is already a parameterised constructor and not a no argument constructor then compiler will not include default constructor it will raise compile a time error.
Program to show use of constructor
1 2 3 4 5 6 7 8 9 10 11 | class Student {// class Student } public class StudnetTest { public static void main(String arg[]) { Student obj1 = new Student();// object created and constructor call } } |
No constructor provided in class Student. while creating object we are calling Student() (default constructor) of Student class.
The default constructor is provided by the compiler.
3 Constructors with Arguments
In
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | class Student { private long rollNo; private String name; Student()//student default constructor { System.out.println("Inside Default Constructor"); } Student(long r)// student one argument constructor { rollNo = r; System.out.println("Inside One Agrument Constructor"); System.out.println("Roll No ="+rollNo); } Student(long r, String n)//student two argument constructor { rollNo = r; name=n; System.out.println("Inside Two Agrument Constructor"); System.out.println("Roll No ="+rollNo+" Name="+name); } } public class StudnetTest { public static void main(String arg[]) { Student obj1 = new Student();// called default constructor Student obj2 = new Student(1);// called one argument constructor Student obj3 = new Student(1, "Ram");// called two argument constructor } } |
Result
1 2 3 4 5 6 7 8 9 | Inside Default Constructor Inside One Agrument Constructor Roll No =1 Inside Two Agrument Constructor Roll No =1 Name=Ram |
Note: In the above program we have created class Student and three constructors Student(), Student(long r) and Student(long r, String n ).
When the line Student obj1 = new Student();
executes and immediately after the object “obj1” is create, Student() constructor will be called.When The constructor line Student obj2 = new Student (1);
executes and immediately after the object “obj2” is create, Student(long r) constructor will be called and passes the value 1 to the constructor.When line “ Student obj3 = new Student(1,"Ram");
executes and immediately after the object “obj3” is create, Student(long r, String n) constructor will be called and passes the value 1 and “Ram” to the constructor.4 Java Constructor Overloading
Like a method’s, we can create a more than one constructor with same name as the class name.
But each constructor has a different parameter. Such constructor is dfcalled
Syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class ClassName { ClassName(int a) { // overloaded constructor } ClassName(int a) { // overloaded constructor } ClassName(char c) { // overloaded constructor } ClassName(int a, int b) { // overloaded constructor } } |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import java.math.BigDecimal; class Employee { long id; String name; String department; BigDecimal salary; Employee() { } Employee(long id) { this.id = id; } Employee(long id, String name) { this.id = id; this.name = name; } Employee(long id, String name, String department) { this.id = id; this.name = name; this.department = department; } Employee(long id, String name, String department, BigDecimal salary) { this.id = id; this.name = name; this.department = department; this.salary = salary; } public void display() { System.out.println("Employee Details"); System.out.println("id=" + id + " name=" + name + " department=" + department + " salary=" + salary); } } public class TestEmployee { public static void main(String args[]) { Employee employee = new Employee(); employee.display(); Employee employee1 = new Employee(1); employee1.display(); Employee employee2 = new Employee(1, "Ram"); employee2.display(); Employee employee3 = new Employee(1, "Ram", "IT"); employee3.display(); Employee employee4 = new Employee(1, "Ram", "IT", new BigDecimal(80000)); employee4.display(); } } |
1 2 3 4 5 6 7 8 9 10 | Employee Details id=0 name=null department=null salary=null Employee Details id=1 name=null department=null salary=null Employee Details id=1 name=Ram department=null salary=null Employee Details id=1 name=Ram department=IT salary=null Employee Details id=1 name=Ram department=IT salary=80000 |
Explanation: In the above program inside class “Employee”, we have created five constructors and each constructor have the different parameter list.
Such constructors are called the overloaded constructor and the process of creating multiple constructors with the same name with different parameter list called constructor overloading.