Java-'this' keyword in java with example

this is a keyword in Java. It can be used inside the method or constructor of a class. It(this) works as a reference to the current object, whose method or constructor is being invoked. This keyword can be used to refer to any member of the current object from within an instance method or a constructor.

Following are the ways to use the ‘this’ keyword in java :

1. Using ‘this’ keyword to refer to current class instance variables
2. Using this() to invoke the current class constructor
3. Using ‘this’ keyword to return the current class instance
4. Using ‘this’ keyword as a method parameter
5. Using ‘this’ keyword to invoke the current class method
6. Using ‘this’ keyword as an argument in the constructor call

Using ‘this’ keyword to refer to current class instance variables


//Java code for using 'this' keyword to
//refer current class instance variables
public class KowledgeFactoryDemo {

int a;
int b;

// Parameterized constructor
KowledgeFactoryDemo(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);
}

public static void main(String[] args) {
KowledgeFactoryDemo object =
new KowledgeFactoryDemo(50, 70);
object.display();
}
}

Output: a = 50  b = 70



Using this() to invoke current class constructor


// Java code for using this() to
// invoke current class constructor
public class KowledgeFactoryDemo {

int a;
int b;

// Default constructor
KowledgeFactoryDemo() {
this(50, 60);
System.out.println("Inside default constructor \n");
}

// Parameterized constructor
KowledgeFactoryDemo(int a, int b) {
this.a = a;
this.b = b;
System.out.println("Inside parameterized constructor");
}

public static void main(String[] args) {
KowledgeFactoryDemo object = new KowledgeFactoryDemo();
}
}

Output:

Inside parameterized constructor
Inside  default constructor 


Using ‘this’ keyword to return the current class instance


//Java code for using 'this' keyword
//to return the current class instance
public class KowledgeFactoryDemo {

int a;
int b;

// Default constructor
KowledgeFactoryDemo() {
a = 60;
b = 80;
}

// Method that returns current class instance
KowledgeFactoryDemo get() {
return this;
}

// Displaying value of variables a and b
void display() {
System.out.println("a = " + a + " b = " + b);
}

public static void main(String[] args) {
KowledgeFactoryDemo object = new KowledgeFactoryDemo();
object.get().display();
}
}

Output:a = 60  b = 80


Using ‘this’ keyword as a method parameter

// Java code for using 'this' keyword as method parameter

public class KowledgeFactoryDemo {

int a;
int b;

// Default constructor
KowledgeFactoryDemo() {
a = 80;
b = 100;
}

// Method that receives 'this' keyword as parameter
void display(KowledgeFactoryDemo obj) {
System.out.println("a = " + a + " b = " + b);
}

// Method that returns current class instance
void get() {
display(this);
}

public static void main(String[] args) {
KowledgeFactoryDemo object = new KowledgeFactoryDemo();
object.get();
}
}

Output: a = 80  b = 100


Using ‘this’ keyword to invoke the current class method

// Java code for using this to invoke current
// class method
public class KowledgeFactoryDemo {
void display() {
// calling fuction show()
this.show();
System.out.println("Inside display function");
}
void show() {
System.out.println("Inside show funcion");
}
public static void main(String args[]) {
KowledgeFactoryDemo obj = new KowledgeFactoryDemo();
obj.display();
}
}

Output:

Inside show function
Inside display function

Using ‘this’ keyword as an argument in the constructor call

// Java code for using this as an argument in constructor
// call
// Class with object of Class B as its data member
public class KowledgeFactoryDemoA {
KowledgeFactoryDemoC obj;
// Parameterized constructor with object of KowledgeFactoryDemoC
// as a parameter
KowledgeFactoryDemoA(KowledgeFactoryDemoC obj) {
this.obj = obj;
// calling display method of class KowledgeFactoryDemoC
obj.display();
}
public static void main(String[] args) {
KowledgeFactoryDemoC obj = new KowledgeFactoryDemoC();
}
}
class KowledgeFactoryDemoC {
int x = 5;
// Default Contructor that create a object of KowledgeFactoryDemoA
// with passing this as an argument in the
// constructor
KowledgeFactoryDemoC() {
KowledgeFactoryDemoA obj = new KowledgeFactoryDemoA(this);
}
// method to show value of x
void display() {
System.out.println("Value of x in Class B : " + x);
}

}

Output: Value of x in Class B: 5

 Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments

  1. Join the Java Jobs Group to get more Java Job Opportunities: https://chat.whatsapp.com/HLFm93tkHQR4zu8hafpqSl

    ReplyDelete
  2. Genuine and Free jobs!
    Don't miss any job opportunities!
    Improve your life, only one step!

    More Java jobs info, Please join the Java Jobs group:
    https://chat.whatsapp.com/HLFm93tkHQR4zu8hafpqSl

    ReplyDelete

Post a Comment