I am working on the World of Zuul game. My question is about these three classes: Room, Inventory and Game. The game class uses an instance of Room called currentRoom. In Inventory Class, I want to write a method to check if a certain item is present in the currentRoom. The thing is that currentRoom is defined in Game, is there a way to use the same instance "currentRoom" in Inventory without having to create an instance of Game in Inventory (which I think is not a good practice as Game is the main class)


Two possible approaches:
1. Make the currentRoom variable static and public, so other code can reference it as Game.currentRoom. - easy to do, but horrible misuse of O.O. practice. Not recommended!
2. Presumably there's some code in the Game class that creates new Inventory and Room instances. At that point it can pass the Game instance ("this") as a parameter to the new Inventory instance. Now the Inventory instance has a reference to the Game instance and can use that to call a simple getCurrentInstance() method from the Game class. This is a good use of O.O., and a very standard way to do this.

following approach 2: when I want to call the getCurrentInstance() from the Game class, is it Game.getCurrentInstance(); or? because what I have done is this:

//creat inventory

    playerInventory = new Inventory(this);

and i didnt change anything in the Inventory constructor!

Thank you JamesCherril for the prompt response!

In the constructor you need to take a copy of the Game instance, so you can use that later to call it
s methods to get the current room or whatever...

Game {
 new Inventory(this);

Inventory {
   Game theGame;
   Inventory(Game g) {
      theGame = g;
   ... theGame.getCurrentRoom() ...