0

I'm supposed to be creating a game, and we've been given free rein in creating it...but I have no idea what I'm doing. I copied the format of a previous lab, because I needed a way to run it, but now I have a problem. My Gui class is as follows:

public class Gui implements Runnable {
    public Gui() { }
    ArrayList<Tile> _al;

    @Override
    public void run() {
          //where I want the _al variable to be used
    }
}

The code I copied used a Driver class to run this method:

public class Driver {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Gui());
    }
}

We haven't discussed this method in class, but from Googling I understand that this is necessary for the program to work. There's been a massive jump in the difficulty of the labs, so I am now lost. I'm sure there's a simple way to format all the code, but I have no idea what the relationship is between the run method and the main, or how I can make it so an association relationship can be made in my code. Nothing I've searched really helped, so I hope this question's okay.

How to associate an instance variable if all the code is inside a run() method?

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
  • 1
    What's stopping you from using the _al variable within your run method? Myself, I'd not make Gui implement Runnable, but rather would create an anonymous inner Runnable class that then creates and initializes the "real" GUI objects. – Hovercraft Full Of Eels Nov 15 '15 at 02:19
  • 1
    What @HovercraftFullOfEels said; this is not because you implement an interface whose sole method has no arguments that the _implementations_ of this interface cannot have state variables, in this case arguments to your constructor. – fge Nov 15 '15 at 02:20
  • "The method run(ArrayList al) of type Gui must ovverride or implement a supertype method." I suppose I'll look into what an anonymous inner Runnable class means. –  Nov 15 '15 at 02:24
  • Maddox -- don't pass _al into the run method as it's already in the class and does not need to be passed in. Just use it. – Hovercraft Full Of Eels Nov 15 '15 at 02:33
  • That's so strange. I could've sworn it gave an error whenever I tried to use _al. So is the run method an exception to that rule? I can't remember ever using an instance variable inside a class without first writing it inside the parameter list, but this seems to be working. –  Nov 15 '15 at 02:59
  • @MaddoxJKingsley you should accept one of the answers here. That's the deal on SO. If someone answers your question you accept it. Usually you upvote it as well. Failing to do that will damage your reputation and discourage people from helping you. – Alain O'Dea Nov 15 '15 at 12:40

2 Answers2

1

You can make the Gui take an constructor argument to allow shared state updates:

public class Gui implements Runnable {
    private final List<Tile> tiles;

    public Gui(List<Tile> tiles) {
        this.tiles = tiles;
    }

    @Override
    public void run() {
          tiles.add(new Tile());
    }
}

And modify the Driver to use that:

public final class Driver {
    public static void main(String[] args) {
        List<Tile> tiles = new ArrayList<Tile>();
        SwingUtilities.invokeLater(new Gui(tiles));
    }
}
Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
0

You've done it right. You can use _a1 in the run method.

_a1 is an instance variable. You can tell this because it does not have the static modifier. Likewise, run is an instance method. Therefore it has access to _a1. Each new instance of Gui gets its own instance of _a1 however.

If that's not what you want, the absolute simplest fix is to add the static modifier to _a1. Then it will be shared across all instances of Gui and can still be accessed from run. I believe Swing does not run runnables concurrently so there shouldn't be concerns with parallel access.

djechlin
  • 59,258
  • 35
  • 162
  • 290