Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes

Shadowing this

When an inner class refers to this, it refers to the current instance of the inner class. To refer to the instance of the outer class from the inner class, this must be qualified by the name of the outer class.

Example of referring to outer this from inner class

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
// ShadowThis.java -- Illustrate shadowing of this.
// Fred Swartz - 2003 - Jun

class ShadowThis {
    public static void main(String[] args) {
        ShadowThis st = new ShadowThis();
        st.testOuter();
    }//end main
    
    private void testOuter() {
        System.out.println(this);
        InnerShadowThis ist = new InnerShadowThis();
        ist.testInner();
    }//end test
    
    class InnerShadowThis {
        void testInner() {
            System.out.println(this);
            System.out.println(ShadowThis.this);
        }
    }//end class InnerShadowThis
}//end class ShadowThis
Produces the following output.
ShadowThis@601bb1
ShadowThis$InnerShadowThis@ea2dfe
ShadowThis@601bb1

Typical usage

The above example fails to show why referencing an outer this might be used. A common situation is to implement an action listener as an inner class of a JPanel. If the listener wants to display a dialog (eg, a JFileChooser), it may tell which component to center the dialog over. The component is exactly the outer this. For example, this is from an inner class listener that wants to center a file dialog over the current TestGUI panel.
int retval = fileChooser.showOpenDialog(TestGUI.this);
Some IDEs put no code in an inner class listener except a call to an outer method to process the action. The same code could be used, but the need to refer to the outer this is no longer necessary.
Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]