0% found this document useful (0 votes)
16 views24 pages

Constructor and This

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views24 pages

Constructor and This

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

JAVA Programming

Course Instructor: Dr. Suvojit Dhara


School of Computer Science
UPES Dehradun
TOPICs to be discussed

 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.

Rules for creating Java constructor:


1. Constructor name must be the same as its class name.
2. A Constructor must have no explicit return type.
3. A Java constructor cannot be abstract, static, final.
Types of Java Constructor

Java Constructors are of mainly two types:


 Default Constructor
 Parameterized Constructor
Default Constructor
 A constructor is called “Default Constructor” when it doesn't have any parameter.

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.

Why use the parameterized constructor?


The parameterized constructor is used to provide different values to distinct objects. However,
you can provide the same values also.
class Student{
int id; String name;
Student(int i, String n){
id = i; name = n;
}
Output:
void display(){ System.out.println(id+" "+name);
} 111
public static void main(String args[]) { Karan
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan"); 222 Aryan

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[]){

 By copy constructor, Student s1 = new Student(111,"Karan");


 By assigning the values of one Student s2 = new Student(s1);
s1.display();
object into another, s2.display();
 By clone() method of Object }
}
class
Output: 111 Karan
111
Karan
Copying object without Copy
Constructor
class Student{ class Student{
int id; String name; int id; String name;
Student(int i, String n){ Student(int i, String n){
id = i; name = n; } id = i; name = n; }
Student(){} void display(){
void display(){ System.out.println(id+" "+name);
System.out.println(id+" "+name); }
} public static void main(String args[]){
public static void main(String args[]){ Student s1 = new Student(111,"Karan");
Student s1 = new Student(111,"Karan"); Student s2 = (Student) s1.clone();
Student s2 = new Student(); s1.display();
s2.id=s1.id; s2.display();
s2.name=s1.name; }
s1.display(); }
s2.display();
}
} Output: 111 Karan
111
Output: 111 Karan Karan
111
‘this’ keyword
 In Java, ‘this’ is a reference variable that refers to the current object.

Usage of java this keyword:


 this can be used to refer current class
instance variable.
 this can be used to invoke current class
method (implicitly)
 this() can be used to invoke current class
constructor.
 this can be passed as an argument in the
method call.
 this can be passed as argument in the
constructor call.
 this can be used to return the current class
instance from the method.
this : to refer current class instance
variable
class Student{
int rollno; String name;
Student(int rollno, String name){

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(){

System.out.println(this.rollno+" "+ System.out.println(rollno+" "+name); }


this.name); } }
} class Test{
class Test { public static void main(String args[])
public static void main(String args[]) {
{ Student s1=new Student(111,
Student s1=new Student(111, "Ankit");
"Ankit"); Student s2=new Student(112,
Student s2=new Student(112, "Sumit");
"Sumit"); s1.display();
s1.display(); Output: s2.display();
111 Ankit
Output: 111 Ankit
s2.display(); }
} } 112
112
} Sumit
this: to invoke current class method
 You may invoke the method of the current class by using this keyword.

 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{

public static void main(String args[]){


A a=new A();
this(): constructor chaining
class Student{
int rollno; String name, course; float fee;
Student(int rollno, String name, String course){
this.rollno=rollno; this.name=name; this.course=course; }
Student(int rollno, String name, String course, float fee){
this(rollno, name, course); //reusing constructor
this.fee=fee; }
void display()
{ System.out.println(rollno+" "+name+" "+course+" "+fee); }
}
class Test {
public static void main(String args[]){
Student s1=new Student(111,"ankit","java");
Student s2=new Student(112,"sumit","python",6000f);
s1.display();
s2.display(); }
}
Output: 111 ankit java 0.0
112 sumit python 6000.0
this: as argument in the method
call
class Student{
void read(Student st){
System.out.println("Student is reading");
}
void study(){
read(this);
}
public static void main(String args[]){ Output:
Student s = new Student();
s.study(); Student is reading
}
}
this: as argument in the constructor
call
class Course {
Student obj;
Course(Student s){ this.obj=s; }
void display(){ System.out.println(obj.id+" "+obj.name); }
}

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

• Constructors in Java (default, parameterized)

• Method vs Constructors

• Copy Constructors in Java

• Constructor Overloading

• ‘this’ keyword and its several usage

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy