-3

So I'm writing a mini-board game program in Java.

The program is going to read in standard input and construct a game as directed in the input.

To help stay organized and progress my oo java skills, I am using a Cell class to act as a cell in a nxn game.

For the board game, I need to have it all in one file, and it must run from static void main.

Here's what my Cell class looks like

public class Cell{
      public int x;
      public int y;
      .
      .
      .
 }

I want to read the input, and assign values to each cell and then add the cells to a list such as ArrayList allCells. However, I can not the use it in a static context.

I understand that static is a single instance, so I'm confused how I would go about doing this. Is there anyway I can use a class-based system to solve this problem. Each cell is it's own individual object, so making it stat wouldn't work.

Any sort-of explanation or alternative would be amazing! Hope I was clear enough in my description.

Joseph
  • 13
  • 1
  • 4
  • Would there be anything wrong with instantiating the `Cell` class, e.g. `Cell cell = new Cell(3, 0);` ? – Tim Biegeleisen Mar 17 '16 at 02:03
  • The main method is there just to give your program an entry point, usually to create a main master class object and perhaps some subsidiary objects, and then set them going. Don't focus on the main method. First focus on the "nouns" of your project. Yes a Cell class is great, but what are you going to put it in? A Grid class perhaps, and what object will run the logic of the program? And what object(s) will interact with the user.... – Hovercraft Full Of Eels Mar 17 '16 at 02:04
  • @TimBiegeleisen I'm a little confused about your question. When parsing over the input, I try to put each piece of information in a cell like that but it can't since it's being called from a static context – Joseph Mar 17 '16 at 02:06
  • `"For the board game, I need to have it all in one file."` -- why? What's the rationale for this requirement? – Hovercraft Full Of Eels Mar 17 '16 at 02:08
  • @HovercraftFullOfEels I have a Game class, similar to what you're describing as a Grid class and it is static. The only problem is that there is a lot of information to put into it. The Cell class was to organize pieces of information together for each cell. I don't want to ignore the main method, because if I get it to function and can't get it to work with main then it was pointless. – Joseph Mar 17 '16 at 02:09
  • @HovercraftFullOfEels not my rationale, to regulate it i guess – Joseph Mar 17 '16 at 02:10
  • `"not my rationale, to regulate it i guess"` -- explain please. If not your rationale, then whose? If your it is your instructor's, then please post the exact wording of the requirement, because you may be misinterpreting it. – Hovercraft Full Of Eels Mar 17 '16 at 02:12
  • @HovercraftFullOfEels I don't think I'm misunderstanding it, it's pretty straight forward lol "single java file, game.java. Within this java file, you must include a public static function, main()" – Joseph Mar 17 '16 at 02:13
  • "You may include any other helper methods or classes within this single file as you see fit." – Joseph Mar 17 '16 at 02:15

1 Answers1

1

The best approach would be to make Cell a top-level class in its own file, but you've indicated that you need everything in a single file. So I'll answer with that constraint in mind.

You need to declare the Cell class itself to be static in order to use it in a static context. For instance:

public class Game {
    public static class Cell { // doesn't really need to be public
        ...
    }

    public static void main(String[] args) {
        Cell c1 = new Cell();
        Cell c2 = new Cell();
        ...
    }
}

Without the static modifier for the Cell class, you will get a compiler error when calling new Cell() inside main() (which I'm guessing is basically the problem you are having).

An alternative is to modify the Cell class to be non-public. Then you can make it a top-level class in the same file as your game class:

public class Game {
    public static void main(String[] args) {
        Cell c1 = new Cell();
        Cell c2 = new Cell();
        ...
    }
}

class Cell {
    ...
}

Yet another alternative would be to make Cell a local class in the main() method:

public class Game {
    public static void main(String[] args) {
        class Cell {
            ...
        }
        Cell c1 = new Cell();
        Cell c2 = new Cell();
        ...
    }
}

However, then you would only be able to use the Cell class in the main() method itself; you could not take advantage of the Cell structure in any other methods of your game.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 2
    Why recommend that he make it static? Wouldn't it be much cleaner to leave it a stand alone class in its own file? – Hovercraft Full Of Eels Mar 17 '16 at 02:07
  • @HovercraftFullOfEels - OP said that everything needed to be in a single file. Perhaps this is an assignment and that's part of the specs. – Ted Hopp Mar 17 '16 at 02:07
  • How could the cell class be static? Doesn't than mean there is only one instance? There needs to be multiple cells for the various coordinates – Joseph Mar 17 '16 at 02:11
  • I do need to access the Cell class in helper methods to perform evaluations – Joseph Mar 17 '16 at 02:12
  • @Joseph - Declaring the `Cell` class as `static` does not mean there's only one instance of `Cell`; that only applies to member fields of a class. When a nested class is _not_ declared `static` then it is an _inner class_ and cannot be instantiated from a `static` context: it needs an instance of the outer class to provide context. See the [Java tutorial on nested classes](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) for mroe details. – Ted Hopp Mar 17 '16 at 02:17
  • @TedHopp Wow thanks, that was what I was confused with. I was under the impression that it applied to classes. Simple fix, thank you – Joseph Mar 17 '16 at 02:20