Constructor and This
Constructor and This
Constructors in Java
Default constructors
Parameterized constructors
Constructor Overloading
Copy Constructors
‘this’ keyword
Let’s START …!!!
Constructors in Java
In Java, a constructor is a block of code similar to the method. It is called when an
object/instance of the class is created. At the time of calling constructor, memory for the
object is allocated.
It is a special type of method which is used to initialize the object. It is called constructor
because it constructs the values at the time of object creation.
Every time an object is created using the “new” keyword, at least one constructor is called.
class Bike{
Bike(){
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike b=new Bike();
}
}
purpose of a default constructor?
The default constructor is used to provide the default values to the object like 0, null, etc.,
depending on the type.
Automatic Default Constructor
If there is no constructor in a class, compiler automatically creates a default
constructor.
class Student{
int id;
String name;
void display(){
System.out.println(id+" "+name);
} Output:
public static void main(String args[]){
Student s1=new Student(); 0 null
Student s2=new Student(); 0 null
s1.display();
s2.display();
}
}
Parameterized Constructor
A constructor which has a specific number of parameters is called a
parameterized constructor.
s1.display();
s2.display();
}
Constructor vs Methods
Constructor Overloading
Constructor overloading in Java is a technique of having more than one constructors with
different parameter lists.
They are arranged in a way that each constructor performs a different task.
They are differentiated by the compiler by the number of parameters in the list and their
types.
class Student{
int id; String name; int age;
Student(int i, String n){
id = i; name = n;
}
Student(int i, String n, int a){ Output:
id = i; name = n; age=a;
} 111
void display(){ System.out.println(id+" "+name+" "+age); } Karan 0
public static void main(String args[]){
Student s1 = new Student(111,"Karan"); 222
Student s2 = new Student(222,"Aryan",25); Aryan 25
s1.display();
s2.display(); }
}
Copy Constructor
class Student{
There is no default copy constructor in int id; String name;
Student(int i, String n){
Java. However, we can copy the values id = i; name = n;
from one object to another by explicitly }
Student(Student s){
defining copy constructor at the time of id = s.id; name =s.name;
class design. }
void display(){
There are many ways to copy the values System.out.println(id+" "+name);
of one object into another in Java. }
public static void main(String args[]){
rollno=rollno; name=name; }
void display(){
Consider the following code where we have
the class fields and the local variables with System.out.println(rollno+" "+name); }
the same name. }
class Test{
public static void main(String args[])
{
Student s1=new Student(111,
"Ankit");
Student s2=new Student(112,
"Sumit");
s1.display();
Output:s2.display();
0 null
}
}
0
null
this : to refer current class instance
variable
class Student{ class Student{
int rollno; String name; int rollno; String name;
Student(int rollno, String name){ Student(int r, String n){
this.rollno=rollno; rollno=r;
this.name=name; } name=n; }
void display(){ void display(){
If you don't use this keyword, compiler automatically adds this keyword while invoking the
method.
this(): to invoke current class
constructor
The this() constructor call can be used to invoke the current class constructor. It is used to
reuse the constructor. In other words, it is used for constructor chaining.
Calling default constructor from
parameterized constructor:
class A{
A()
{System.out.println("hello a");}
A(int x){ Output:
this();
System.out.println(x); }
} hello a
class Test{ 10
public static void main(String args[]){
A a=new A(10);
}
this(): to invoke current class
constructor
The this() constructor call can be used to invoke the current class constructor. It is used to
reuse the constructor. In other words, it is used for constructor chaining.
Calling parameterized constructor from
default constructor:
class A{
A(){
this(10);
Output:
System.out.println("hello a");
}
A(int x){ System.out.println(x); } 10
} hello a
class Test{
class Student{
int id=5000; String name="Subhash";
Student(){ Output:
Course c=new Course(this); c.display();
}
public static void main(String args[]){
5000 Subhash
Student s = new Student();
}
}
this: to return current class instance
class Student{
int id; String name;
Student setStudentData(int x, String n){
id=x; name=n;
return this;
}
void display(){System.out.println(id+" "+name);}
}
Output:
class Test{
public static void main(String args[]){ 111 ankit
Student obj = new Student();
obj = obj.setStudentData(111,"ankit");
obj.display();
}
}
Summary
Today, we learned about
• Method vs Constructors
• Constructor Overloading