Showing posts with label Inner Classes. Show all posts
Showing posts with label Inner Classes. Show all posts

July 18, 2011

Access outer class instance from within the inner class method

MyOuter is the outer class which contains MyInner which is the inner class. The seeOuter() method within the inner class can access the inner class instance with the this keyword. In order to access the outer class instance from within the seeOuter() method, the syntax is use the outer class name with the this keyword - the syntax looks like this: OuterClassName.this

The code example below demonstrates the same,

package info.icontraining.basic;

public class MyOuter {
     private int x = 3;
 
   class MyInner {
      private int x = 5;
  
      public void seeOuter() {
         System.out.println("Inner x is = " + this.x);
         System.out.println("Outer x is = " + MyOuter.this.x );
      }
   }
 
   public static void main(String[] args) {
      MyOuter.MyInner inner = new MyOuter().new MyInner();
      inner.seeOuter();
   }
}

April 1, 2011

Application of Inner Classes in Java - Swing Example

One of the most popular applications of Inner Classes is to handle events in Java to put the event-handling code in a separate event-handing class. This allows separating the event-handling code from the event-generating code. Since the event-handling code is in a separate class, if frees up the event-handling class to inherit from another class.

In the following example code, the class InnerClassExample is the event-generating class (it has JButton object as an instance variable) and the inner class MyEventHandler is the event-handling class (an instance of this class is added to the JButton object as an action listener (or simple, an event-handler).

Since InnerClassExample extends another class, if the InnerClassExample is also implemented as an event-handling class (instead of using an inner class to do the event-handling), it would not be possible to inherit (or reuse) from another class that contains event-handling code.

In the example, MyEventHandler extends TestClass and inherits its methods. The MyEventHandler can then reuse those methods for event-handling. If there was no inner class, then inheriting event-handling code from TestClass would not be possible.


package info.icontraining.basic;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class InnerClassExample extends JFrame  {

   private JButton testButton;
   private JTextArea textArea;
   private JPanel panel;
    
   public InnerClassExample() {
        
      panel = new JPanel (new GridLayout(2,1));

      add(panel, BorderLayout.CENTER);
     
      testButton = new JButton("Test Button");
      testButton.setSize(75, 30);
      panel.add(testButton);
 
      MyEventHandler ic = new MyEventHandler();
      testButton.addActionListener(ic);
  
      textArea = new JTextArea();
      textArea.setSize(100, 50);
      textArea.setVisible(true);
      panel.add(textArea);
     
      setTitle("Inner Class as Event Handler");
   }

   public class MyEventHandler extends TestClass implements ActionListener {
   
      public void actionPerformed(ActionEvent ae) {

         // invoke method inherited from Test Class

         if (ae.getSource().equals(testButton)) {
            textArea.setText("Hello World");    
         }
      }
   }

   public static void main(String[] args) {
  
      InnerClassExample frame = new InnerClassExample();
      frame.setSize(200,200);
      frame.setLocationRelativeTo(null);  
      frame.setVisible(true);
   }
}

February 5, 2011

Inner Classes in Java

Inner classes are classes defined within another class as a member of that class. The inner class code has access to members of the enclosing outer class, including those members marked private.


public class Outer {

   private int i;

   public void makeInner {
      Inner inner = new Inner();
      inner.accessOuter;
   }

   class Inner {
      public void accessOuter {
         i = 7;
         System.out.println("Outer class, i=" + i);
      }
   }

   public static void main(String[] args) {
      Outer outer = new Outer();
      outer.makeInner();

      Outer.Inner inner = new Outer().new Inner();
      inner.accessOuter();
   }
}