'this' reference in Java - GeeksforGeeks
'this' reference in Java - GeeksforGeeks
Xojo, Inc.
Learn More
Java
// Driver Class
public class Person {
// Fields Declared
String name;
int age;
// Constructor
Person(String name, int age)
{
this.name = name;
this.age = age;
}
// main function
public static void main(String[] args)
{
// Objects Declared
Person first = new Person("ABC", 18);
Person second = new Person("XYZ", 22);
first.printDetails();
second.printDetails();
first.change_name("PQR");
Openhas
System.out.println("Name In App
been changed to: "
+ first.get_name());
}
}
Output
Name: ABC
Age: 18
Name: XYZ
Age: 22
Explanation
In the above code, we have defined a Person class with two private fields
name and age. We have defined the Person class constructor to initialize
these fields using this keyword. We have also defined getter and setter
methods for these fields which use this keyword to refer to the current
object instance.
Open In App
In the Main class, we have created two Person objects and called the
printDetails() method on each object. The output shows the name, age,
and object reference of each object instance.
Java
// Parameterized constructor
Test(int a, int b)
{
this.a = a;
this.b = b;
}
void display()
{
// Displaying value of variables a and b
System.out.println("a = " + a + " b = " + b);
}
Output
a = 10 b = 20
Java
// Default constructor
Test()
{
this(10, 20);
System.out.println(
"Inside default constructor \n");
}
// Parameterized constructor
Test(int a, int b)
{
this.a = a;
this.b = b;
System.out.println(
"Inside parameterized constructor");
}
Output
Java
// Default constructor
Test()
{
a = 10;
b = 20;
}
Output
a = 10 b = 20
Java
// Default constructor
Test()
{
a = 10;
b = 20;
}
// main function
public static void main(String[] args)
{
Test object = new Test();
object.get();
}
Open In App
}
Output
a = 10 b = 20
Java
void display()
{
// calling function show()
this.show();
void show()
{
System.out.println("Inside show function");
}
Output
Open In App
Inside show function
Inside display function
Java
class B {
int x = 5;
Output
Value of x in Class B : 5
Although ‘this’ reference comes with many advantages there are certain
disadvantages of also:
1. Overuse of this can make the code harder to read and understand.
2. Using this unnecessarily can add unnecessary overhead to the
program.
3. Using this in a static context results in a compile-time error.
4. Overall, this keyword is a useful tool for working with objects in Java,
but it should be used judiciously and only when necessary.
Advertise with us
static Keyword in Java
Similar Reads
8 min read
11 min read
10 min read
8 min read
Java Interface
An Interface in Java programming language is defined as an abstract type used to specify the
behavior of a class. An interface in Java is a blueprint of a behavior. A Java interface contains static…
9 min read
Encapsulation in Java
Encapsulation in Java is a fundamental OOP (object-oriented programming) principle that combines
data and methods in a class. It allows implementation details to be hidden while exposing a public…
[ ]
7 min read
Inheritance in Java
Java, Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in
Java by which one class is allowed to inherit the features(fields and methods) of another class. In…
12 min read
Abstraction in Java
Abstraction in Java is the process of hiding the implementation details and only showing the essential
functionality or features to the user. This helps simplify the system by focusing on what an object…
9 min read
6 min read
Polymorphism in Java
The word ‘polymorphism’ means ‘having many forms’. In Java, polymorphism refers to
the ability of a message to be displayed in more than one form. This concept is a key feature of…
6 min read
13 min read
6 min read
9 min read
6 min read
Open In App
Corporate & Communications
Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar
Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida,
Gautam Buddh Nagar, Uttar Pradesh,
201305
Advertise with us
Open In App
Company Languages
About Us Python
Legal Java
Privacy Policy C++
In Media PHP
Contact Us GoLang
Advertise with us SQL
GFG Corporate Solution R Language
Placement Training Program Android Tutorial
GeeksforGeeks Community Tutorials Archive