1

How do I pass a object to an implement and pass the local object to object that is outside? I think the SwingUtilities.invokeLater is nessasary for a Swing object , right?

  Sensors sens = new Sensors();

  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    GUI application = new GUI(sens);
    application.getJFrame().setVisible(true);
   }
  });

  SMS sms = new SMS(application);

this is me try to solve the problem , but i get a 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). problem.

// in main
Sensors sens = new Sensors();
GUI application = null;
SwingUtilities.invokeLater(new GUIthread(sens , application));
SMS sms = new SMS(application);


//a class inside GUI.java , but not inside GUI class
class GUIthread implements Runnable{
  Sensors s;
  GUI g;
  public GUIthread(Sensors s , GUI g){
   this.s = s;
   this.g = g;
  }

  @Override
  public void run() {
   // TODO Auto-generated method stub
   g = new GUI(s);
   g.getJFrame().setVisible(true);
  }
 }

the sourcecode

wizztjh
  • 6,979
  • 6
  • 58
  • 92

4 Answers4

2

This problem arises when you try to create an instance of a non-static inner class in a context that does not specify (or imply) an instance of the enclosing class.

From this, I deduce that you have declared one of your classes as a non-static inner class; e.g. something like this:

public class Outer {
    ...
    public class Inner {
        public Inner() {
           ...
        }
        ...
    }
    ...
}

If you now try to create an instance of Inner in some other code using new Inner(), you will get a compilation error like the one you are seeing.

You can do one of two things to "fix" the problem:

  • If you change public class Inner { to public static class Inner {, you can use new Inner() as you are currently doing. But this will mean that the code of Inner cannot access the (final) instance variables of the enclosing class; i.e. Outer.

  • If you don't want to change Inner to a static class, you will need to instantiate it as follows:

    Outer outer = ...
    ...
    Inner inner = outer.new Inner();  // qualified creation
    

FOLLOWUP

any down side using static class to call swing?

Only the one that I identified above.

SO , all the instantiate happen inside Outer constructor? right?

No. The code in the "qualified creation" example above can appear anywhere that the Inner class is accessible. And since we declared it as public ...

If you instantiate Inner inside a constructor (or instance method) for Outer, you can just use new Inner(). The enclosing Outer instance is the same as this.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • any down side using static class to call swing? SO , all the instantiate happen inside Outer constructor? right? – wizztjh Jan 20 '11 at 04:56
0

Try

final Sensors sens = new Sensors();

instead.

Jochen Bedersdorfer
  • 4,093
  • 24
  • 26
0

Easy, declare the reference final and it will be seen by the anon class code.

Alice Young
  • 1,742
  • 2
  • 11
  • 19
  • This is not the problem. If it was, the OP would see an compilation error at a different point complaining about use of a non final reference in an enclosing class. – Stephen C Jan 20 '11 at 04:48
0

I agree with Zach and suspect that GUIthread is an inner class. If so, you may do well to make it a stand-alone class or a static inner class, but it's difficult to know if this is the true solution without more information and without the actual error message.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373