views:

123

answers:

3

Okay, this is a bit messy:

I'm using Netbeans, and I have a main class called ParameterUI. (This is a GUI) This class has a few sliders on its GUI, and since these are private, I have a method called getBounds(). I don't want to clutter up my GUI, and so essentially all the important methods for calculating stuff are in another class called Structure. So ParameterUI calls a method in Structure, which calls another few methods inside itself, and one of these calls getBounds.

The problem is that getBounds can't be static, but I can't call it if it isn't.

In ParameterUI.class :

public int[] getBounds () {
    int[] bounds = new int[2];
    bounds[0] = jSlider2.getMinimum();
    bounds[1] = jSlider2.getMaximum();
    return bounds;
}

In Structure.class :

private static void myMethod (Graphics g, double[] planet, long mass) {
    int[] bounds = ParameterUI.getBounds(); //<-- doesn't work
}

Making myMethod non-static doesn't seem to help either. I'm afraid that while I know the basics about static vs. non-static, I haven't been programming with classes etc. for that long.

Edit: Essentially, I know what the problem is, and I'm looking for a better way to solve it.

+1  A: 

Pass the ParameterUI instance to the static method

private static void myMethod (ParameterUI param, Graphics g, double[] planet, long mass) {
    int[] bounds = param.getBounds(); //<-- doesn't work
}

However you may want to reconsider a design where you are calling into static methods of other classes in order to calculate things about the first class. This suggests that all of the logic necessary for your UI class is not contained in it, and public static methods lead to hard to test code.

matt b
Thanks! Yeah, I originally thought separating my GUI from the rest of my code would be a good idea, since I'm using the Netbeans feature to make it as opposed to doing it by hand, but it might be easier to migrate all my methods into one class. Passing the ParameterUI instance to that particular static method would actually be quite a pain because of where it is in my program, in any case.
Stella
+1  A: 

Static vs Non-Static

Static means that you can access the method(s) without instantiating an object of that class.

Non-Static means that you can only access the method(s) from an instance of that class.

What you need to do is figure out if you want the methods in the ParameterUI class to be Static or not.

If you change get bounds to be Static, then it will work.

public static int[] getBounds () {
   int[] bounds = new int[2];
   bounds[0] = jSlider2.getMinimum();
   bounds[1] = jSlider2.getMaximum();
   return bounds;
}

You might want to think about it first.

Tony Abrams
Because I haven't created the GUI myself, I can't set getBounds to be static (if I do, it can't access the non-static, private jSliders). I would have liked them to be static...
Stella
+1  A: 

Basics : You can't access non static members from static method.

You will need to create instance or pass instance of ParameterUI to/in static method

YoK