Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java this keyword

    The Java this keyword is a reference variable that refers to the current object within a non-static method or constructor. It is primarily used to eliminate ambiguity between instance variables and method parameters with the same name.
    Using the "this" keyword in Java allows for explicit reference to the instance variables of the current object. It proves particularly valuable in situations where distinction between local variables and instance variables is necessary, or when passing the current object as a parameter to another method or constructor is desired.


Table Of Content

  • ‘this’ keyword can be used to refer current class instance variables
  • ‘this’ keyword can be used to invoke current class constructor
  • ‘this’ keyword can be used to return the current class instance
  • ‘this’ keyword can be used as method parameter
  • ‘this’ keyword can be used as an argument in the constructor call
  • ‘this’ keyword can be used to invoke current class method






Usage of Java this keyword

There are 6 uses of this keyword in java

  • ‘this’ keyword can be used to refer current class instance variables
  • ‘this’ keyword can be used to current class constructor
  • ‘this’ keyword can be used to return the current class instance
  • ‘this’ keyword can be used as method parameter
  • ‘this’ keyword can be used to invoke current class method
  • ‘this’ keyword can be used as an argument in the constructor call

1.this-keyword-can-be-used-to-refer-current-class-instance-variables


The keyword this can be used to refer to the current class instance variable. If there is any discrepancy between the instance variables and arguments, this keyword resolves it.


Java program or using 'this' keyword to refer current class instance variables
 // Java program or using 'this' keyword to refer current class instance variables  
class StaticTest
{
    int x;
    int y; 
    StaticTest(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    void display()
    {
        System.out.println("x : " + x + "  y : " + y +"\n\nX+Y="+(x+y));
    }
 
    public static void main(String[] args)
    {
    	StaticTest object = new StaticTest(5, 5);
        object.display();
    }
}




Output:

x : 5  y : 5 

 
X+Y=10

2.‘this’ keyword can be used to invoke current class constructor


To invoke the current class constructor, use the this() constructor call. It allows you to reuse the constructor. In other words, it is used to link constructors.


Java program using 'this' keyword used to invoke current class constructor
 // Java program  using 'this' keyword used to invoke current class constructor  
class StaticTest
{
    int x;
    int y; 
    public StaticTest() {
		// TODO Auto-generated constructor stub
    	this(10, 10);
    }
    StaticTest(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    void display()
    {
        System.out.println("x : " + x + "  y : " + y +"\n\nX+Y="+(x+y));
    }
 
    public static void main(String[] args)
    {
    	StaticTest object = new StaticTest(); // here no need to here give value 
        object.display();
    }
}



Output:

x : 10  y : 10 

 
X+Y=20

3.‘this’ keyword can be used to return the current class instance


To invoke the current class constructor, use the this() constructor call. It allows you to reuse the constructor. In other words, it is used to link constructors.


Java program using 'this' keyword used to invoke current class instance
 // Java program  using 'this' keyword used to invoke current class instance  
class StaticTest
{
    int x;
    int y;
    StaticTest(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    StaticTest get()
    {
        return this;
    }
    void display()
    {
        System.out.println("x : " + x + "  y : " + y +"\n\nX+Y="+(x+y));
    }
 
    public static void main(String[] args)
    {
    	StaticTest object = new StaticTest(10, 10); // here no need to here give value 
    	object.get().display();
    }
}



Output:

x : 10  y : 10 
 
X+Y=20

4.‘this’ keyword can be used as method parameter


The keyword this can also be used as an argument in a method. It is primarily used in event handling.


Java program using ‘this’ keyword as method parameter
 // Java program  using ‘this’ keyword  as  method parameter  
class StaticTest
{
    int x;
    int y;
    StaticTest(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    void display(StaticTest test)
    {
        System.out.println("x : " +test.x + "  y : " + test.y);
    }
   
    void p() // this method return current class instance
    {
        display(this);
    }
 
    public static void main(String[] args)
    {
    	StaticTest object = new StaticTest(10, 10); // here no need to here give value 
    	object.p();
    }
}



Output:

x : 10  y : 10 

5.‘this’ keyword can be used as an argument in the constructor call


To invoke the current class constructor, use the this() constructor call. It allows you to reuse the constructor. In other words, it is used to link constructors.


Java program using ‘this’ keyword as method parameter
 // Java program  using ‘this’ keyword  as  method parameter  
class StaticCheck{  
  StaticTest obj;  
  StaticCheck(StaticTest obj){  
      this.obj=obj;  
    }  
    void display(){  
     System.out.println(obj.x); 
    }  
}

class StaticTest{  
  String x="Alok";  
  StaticTest(){  
  StaticCheck staticCheck=new StaticCheck(this);  
  staticCheck.display();  
  }  
  public static void main(String args[]){  
	  StaticTest object=new StaticTest();  
  }  
} 



Output:

Alok 

6.‘this’ keyword can be used to invoke current class method


Java program using ‘this’ keyword to invoke current class method
 // Java program  using ‘this’ keyword to  invoke current class method  
class StaticTest{  
    String x="Alok";  
  void display()
  {    
      this.show(); // this code will call  show() method
      System.out.println(x);
  }   
  void show() {
      System.out.println("I am Show method");
  } 
  public static void main(String args[]){  
	  StaticTest object=new StaticTest(); 
	  object.display();
  }  
}



Output:

I am Show method 
Alok 

Java Inheritance Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.