2

I am trying to use an inner class setup, however I'm having some problems. This is the code I'm trying to use:

public class GUI
{
   class ButtonHandler implements ActionListener
   {
       public void actionPerformed(ActionEvent e)
       {
         // do something
       }
   }

   private static void someMethod()
   {
      JButton button = new JButton( "Foo" );
      button.addActionListener(new ButtonHandler());
   }
}

This is the error message I'm getting (in eclipse):

No enclosing instance of type GUI is accessible. Must qualify the allocation with an enclosing instance of type GUI (e.g. x.new A() where x is an 
 instance of GUI).

Please can someone help me out?

  • this is an odd example. typically when you make a listener like this you want it to have access to the outer class instance. and components like buttons have to live in another component. Using a static method doesn't make a lot of sense. so even though everybody is telling you to make your inner class static, you may have bigger problems. Look at the Oracle Swing tutorials for examples of how to make Swing GUIs. – Nathan Hughes Oct 20 '11 at 19:37
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:35

6 Answers6

8

Change the declaration from:

class ButtonHandler implements ActionListener

To:

static class ButtonHandler implements ActionListener

Without the "static" modifier, it's an instance-level inner class, which means you need an instance of the enclosing GUI class for it to work. If you make it a "static" inner-class, it acts as a normal top-level class (which are implicitly static).

(And the key reason this is necessary in your example is that someMethod is static, and so you have no instance of the enclosing class in that context.)

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
2

I believe it's giving you this error since this is being done in a static method. Since ButtonHandler is a non-static nested class, it must be tied to an enclosing GUI instance. Most likely you just want a static nested class instead:

static class ButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
      // do something
    }
}
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
1

Make your inner class static. :)

asenovm
  • 6,397
  • 2
  • 41
  • 52
1

To create an instance of a non-static inner class, you need to have an instance of the surrounding outer class (and there is none, because someMethod is static):

JButton button = new JButton( "Foo" );
button.addActionListener(new GUI().new ButtonHandler());

If there's no need for the inner class to access members/methods of the outer class, make the inner class static, then you can create an instance of the inner class like this:

static class ButtonHandler implements ActionListener { ... }

...

JButton button = new JButton( "Foo" );
button.addActionListener(new GUI.ButtonHandler());

(in this case even plain new ButtonHandler() would work because someMethod() is defined in the outer class GUI, i.e. in the same "namespace" as ButtonHandler)

Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
0

you are trying to access a non-static member from within a static member. When you access ButtonHandler inside your static function, the member class is not visible as its associated with an instance of GUI class.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ravi Bhatt
  • 3,147
  • 19
  • 21
0

You should make ButtonHandler a static class. For example:

static class ButtonHandler implements ActionListener

In Java, static inner classes don't need an instance of the container class to instantiate. You can simply do new ButtonHandler() (as you are currently doing).

Just for your information, you could also change the line

button.addActionListener(new ButtonHandler());

to

button.addActionListener(this.new ButtonHandler());

But I wouldn't recommend it.

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77