File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Accessing method calls associated with another class object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing method calls associated with another class object" Watch "Accessing method calls associated with another class object" New topic
Author

Accessing method calls associated with another class object

Jason Lehr
Greenhorn

Joined: Dec 17, 2004
Posts: 4
So I'm making a craps game. I have a class "Player" and a class "Bet". The constructor of player sets the name, bankroll, and creates a new Bet(). My question is, how do I access those Bet methods associated with that player. Specifically, I have a method called "setBet()" that I'm trying to call. For the life of me, I can't remember how to do it. (I tried stupid things like player.bet.setBet() ).

Thanks for any help, I'm sure this one will be easy for you guys to answer.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

The answer depends on your code. If the Bet is in a non-private member of Player named "bet", then player.bet.setBet() could be exactly right. But there are many other possibilities, so you're going to have to show us what you've got. Please use "CODE" tags to format your code (see the "Instant UBB Code" area on the message editing screen.)


[Jess in Action][AskingGoodQuestions]
Jason Lehr
Greenhorn

Joined: Dec 17, 2004
Posts: 4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

In this case, the answer is that it's impossible. "bet" is a local variable in Player's constructor, and as soon as the constructor exits, the Bet object is eligible for garbage collection.

You need to make the Bet object be a member like "name" and "bankroll", and provide a getBet() method; then you can write aPlayer.getBet().setBet(...) .
Jason Lehr
Greenhorn

Joined: Dec 17, 2004
Posts: 4
ahhh, thanks! I knew I was missing something.
Jason Lehr
Greenhorn

Joined: Dec 17, 2004
Posts: 4
What do I need to put in the getBet() method? More than just a return of the bet object right?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing method calls associated with another class object