views:

156

answers:

4
package gui;


public class Solver {


    void solveIt(){
        CubeGui.moveThat();
    }    

}

I am trying to access method moveThat from this class, but it keeps telling me cant access non static method moveThat from a static reference. I don't see how this is a static reference?

+6  A: 

How is CubeGui defined? The spelling suggests it’s a class name, in which case moveThat would have to be static to be accessed, since you didn’t create an instance of CubeGui.

Konrad Rudolph
it's defined like this "public class CubeGui extends javax.swing.JFrame"CubeGui is a swing interface so I don't want to create another instance of it. I thought it would be accesible from another public non static class without making another instance. Is there any way other than make moveThat() static?
kokokok
I think what he means is, "How is the `moveThat` method defined?" If `moveThat` is non-static, you will need to either make it static, or create an instance of `CubeGui` in order to call it.
ph0enix
It sound like you want to pass the instance of CubeGui to this other class rather than make "moveThat" static.
mlk
@ph0enix: no, I actually meant what I wrote but your question would be valid as well.
Konrad Rudolph
+3  A: 

moveThat is an instance method.

  public class Solver {
        void solveIt(){
            new CubeGui().moveThat();    
        }
    }
adatapost
+4  A: 
ph0enix
If I don't want to make it static, and go with the second option wouldn't I end up with two different instances of Gui, then solver won't be acting on my original Gui at all? Is there some way in which I can make outside public classes access public methods of the same instance of my Gui??
kokokok
Yes, that is correct, my apologies for shortcut-ing my example... To operate on the same instance of `CubeGui`, you would need to pass in the instance as a parameter to the `moveThat` method.
ph0enix
A: 

From your comments it sounds like CubeGui is a swing frame that you only open once, and you want to get a reference to that frame to call the moveThat method. The best way would be to pass a reference to your solver class:

public class Solver {

private CubeGui gui;

public Solver(CubeGui gui) {
     this.gui = gui;
}

void solveIt(){
    gui.moveThat();
}

}

If that is really difficult in practice, you could consider making CubeGui a singleton (as much as that is rightly discouraged, sometimes it makes sense, as in this case where you don't have more than one frame showing). To do this, you make the constructor of CubeGui private, and then add a method to CubeGui like this:

 private static CubeGui singleInstance;

 public static CubeGui getInstance() {
     if (singleInstance == null) {
         singleInstance = new CubeGui();
     }
     return singleInstance;
 }

Then in your solver you can do:

  CubeGui.getInstance().moveThat();

I'll probably get a downvote for suggesting a Singleton. I might even deserve it, but it sounds like the question needs to know what that looks like. To counter balance that, see here.

So if at all possible, pass the object in as a parameter, either to the constructor, or to the method.

Yishai
you did understand my problem correctly, cubeGui is a single swing frame which I don't want more than one of, and I also don't want to put all kind of different methods in one class so I am trying to access the frame from different classes without making stuff static.could you please explain how passing a reference to solver class will work? It looks like you have created a new gui instance, wouldn't that open a whole new frame?the singleton is a new concept to me, I am using netBeans editor to create the frame, I'll see if it lets me modify the constructor.
kokokok
In order to pass a reference to the solver class you have to retain the reference when you first call the frame's constructor, so it would become a reference that gets passed around a lot.
Yishai