简体   繁体   中英

Having a issue using inner class (java)

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?

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.)

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
    }
}

Make your inner class static. :)

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 , ie in the same "namespace" as ButtonHandler )

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM