Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaDifference Between Class.this and this in Java

Difference Between Class.this and this in Java

In java, Class.this and this might refer to the same or different objects depending upon the usage.

this

this is a reference variable that refers to the current object. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Class.this

Class.this is a reference variable that refers to the outer class object inside a nested class or anonymous methods. If there is ambiguity between the inner (or anonymous) class and outer class, Class.this resolves the problem of ambiguity.

If there are no nested (or anonymous) classes, this and Class.this refer to the same object.

Example Code

Consider the code given below where both this and Class.this refer to the same object. 

Java




class Outer
{
    public void showOuter()
    {
          System.out.println ("this = " + this);
        System.out.println ("Outer.this = " + Outer.this);
    }
    
    public static void main (String[] args) 
    {
        Outer outer = new Outer();
        outer.showOuter();
    }
}


Output

this = Outer@b4c966a
Outer.this = Outer@b4c966a

In the code given below, an Inner nested class has been added. In this example, this and Outer.this point to different objects.

Java




class Outer
{
      class Inner
    {
        public void showOuter()
        {
            System.out.println ("Outer.this = " + Outer.this);
        }
        
        public void showInner()
        {
              System.out.println ("this = " + this);
        }
    }
    
    public static void main (String[] args) 
    {
        Outer outer = new Outer();
        Inner inner = outer.new Inner();
          
          inner.showOuter();
         inner.showInner();     
    }
}


Output

Outer.this = Outer@2f4d3709
this = Outer$Inner@4e50df2e

In the code given below, usage of class.this inside anonymous method has been demonstrated. In this example too, class.this and this point to separate objects. For the purpose of demonstration, JButton has been used and its doClick() method has been invoked.

Java




import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
  
class GFG
{
      public void createAndFireButton()
    {
         JButton button = new JButton();
        
           button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println ("GFG.this = " + GFG.this);
                System.out.println ("this = " + this);
            }
        });
  
        button.doClick();
    }
    
       public static void main(String[] args)
    {    
          GFG gfg = new GFG();
        gfg.createAndFireButton();
    }
}


Output

GFG.this = GFG@1fbc7afb
this = GFG$1@45c8e616

Note: Please note that when using lambda expressions, class.this and this both refer to the same outer class reference. 

In the example given below, instead of using an anonymous class, we have used lambda to add an action listener to JButton.

Java




import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
  
class GFG
{
      public void createAndFireButton()
    {
         JButton button = new JButton();
  
            button.addActionListener(e -> {
                  System.out.println ("GFG.this = " + GFG.this);
                System.out.println ("this = " + this);
            });
  
            button.doClick();
    }
    
       public static void main(String[] args)
    {    
          GFG gfg = new GFG();
        gfg.createAndFireButton();
    }
}


Output

GFG.this = GFG@1fbc7afb
this = GFG@1fbc7afb
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments