This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Getting from another class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting from another class." Watch "Getting from another class." New topic

Getting from another class.

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
What is the easiest way to get something from another class in the project and using it in the class you want. For example: currentRoom is an object of Room in the Game class. There is another class called Player... and the player object has to store the currentRoom in the Game class. And... this has to be done in the Player class... its necessary. Whats the easiest way to do this? Even Pseudo code will help as long as the keyword used is in it because chances are, I don't know it. Thanks!!

Live And Let Learn.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11861

Write a method in the Game class called getCurrentRoom() that returns a Room. If you are in Player, then you need a reference to the Game class. if you are in some other code, you could write a putCurrentRoom() in the Player class, then do

Room tempRoom = myGame.GetCurrentRoom();

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Have you checked out Aspose?
subject: Getting from another class.
jQuery in Action, 3rd edition