This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund 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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Getting from another class." Watch "Getting from another class." New topic
Author

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
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

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();
currentPlayer.putCurrentRoom(tempRoom);


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Getting from another class.
 
Similar Threads
First Java Game - Help REALLY appreciated
multiple class iteractions
Null Pointer Exception
Problem with a method in my EJB
Locked door help