1

My understanding is that you can't do what I'm asking. If you look at the starred (*) commented errors in the code below, you can see what I'm trying to access. I feel like I need to be able to do this so that I can use a method to dynamically create many objects and then access all of those objects from other objects.

Is there a way to do this that I'm missing, or am I just messing something up? If not, how should I go about doing this to enable me to get the same functionality as below? If there's any way to do this other than passing the objects around, it would be appreciated (passing objects seems like so much work - especially with multi-dimensional arrays of objects - there should be an easy way to instantiate package-private objects that can be accessed anywhere else in the package). But if passing is the only way, please let me know the best way to do it, especially when I'm passing a two-dimensional array of a bunch of objects. Thanks!

package simpleclasswithinclasstest;

class Game {

    static int boardSize;
    Engine gameEngine;

    Game() {
    }

    public void run() {
        gameEngine = new Engine();
        gameEngine.play();
    }

    public int getBoardSize() {
        return boardSize;
    }
}

class Engine {

    int boardSize;

    Engine() {
    }

    public void play() {

        this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
        // *****It doesn't recognize currentGame, but I want it to.
    }

    void doNothing() {
    }
}

class Board {

    Board() {
    }

    void Test() {
        gameEngine.doNothing(); // 2 *****Error is here.
        // *****It doesn't recognize gameEngine.
    }
}

public class SimpleClassWithinClassTest {

    static Game currentGame;

    public static void main(String[] args) {
        currentGame = new Game();
        currentGame.run();
    }
}
Charles
  • 50,943
  • 13
  • 104
  • 142
Eric Martin
  • 317
  • 2
  • 13

4 Answers4

3

You will get access to gameEngine through your Board class by passing it as a parameter to Board. When you instantiate your Board, you could do something like this:

class Engine {
    int boardSize;

    Engine () {
    Board board = new Board(this);
    }

    public void play() {
    }

    void doNothing() {
    // magic stuff in here
    }
}

class Board {
    Engine engine;

    Board (Engine gameEngine) {
    this.engine = gameEngine
    }

    void Test() {
        engine.doNothing(); // No error here :-) and this engine is your main one
    }
}

Take a look at the concept of message-driven communication. Things might get clearer for you by reading this answer.

In the following picture, which I took from the answer linked above, you can imagine f as your engine object within the Engine class, and c as your engine within the Board class. You are actually manipulating the same object.

enter image description here

As for your other problem (the first one): it can't recognize currentGame because you don't have any variable with that name in your scope.

Community
  • 1
  • 1
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
  • 1
    Thank you. I really appreciate it. I just want to check my understanding. If I passed an int, it would create a new int, but if I pass an object, it still accesses the original object? And is this the same for all primitives vs. reference types? Thanks. – Eric Martin Sep 03 '13 at 02:46
  • In Java it's not possible to pass primitives by reference. You can bypass it by passing a reference to an instance of a mutable wrapper class. Check this answer for further info: http://stackoverflow.com/questions/4319537/how-do-i-pass-a-primitive-data-type-by-reference/4319581#4319581 – Natan Streppel Sep 03 '13 at 02:53
1

There is no variable of any type named 'currentGame' in scope at that point in the code.

Furthermore, while boardSize is a static package-protected variable, the method getBoardSize() is an instance variable. One possible solution is to make the method static and package protected, then you can do this:

public void play() {
    this.boardSize = Game.getBoardSize();
}
caskey
  • 12,305
  • 2
  • 26
  • 27
1

This is like initializing an int variable in 1 function and trying to access it from another function.

The objects(you are trying to access) are out of scope in the part of the code where you are trying to access.

You can resolve this issue by sending this as a parameter and recieving it as an object in the corresponding method.

nj-ath
  • 3,028
  • 2
  • 25
  • 41
1

We can use class reference to call static methods only. So you can make the play a static method.

class Engine {

    int boardSize;

    Engine() {
    }

    public void play() {

        this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
        // *****It doesn't recognize currentGame, but I want it to.
    }

    static void doNothing() {
    }
}

class Board {

    Board() {
    }

    void Test() {
        Engine.doNothing();
    }
}

The other way is to make an Object from the class and access the non static methods within that object.

class Engine {

    int boardSize;

    Engine() {
    }

    public void play() {

        this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
        // *****It doesn't recognize currentGame, but I want it to.
    }

    void doNothing() {
    }
}

class Board {

    Board() {
    }

    void Test() {
        Engine gameEngine = new Engine();
        gameEngine.doNothing();
    }
}
Buddhika Ariyaratne
  • 2,339
  • 6
  • 51
  • 88