Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing method calls associated with another class object

 
Jason Lehr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Jason Lehr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahhh, thanks! I knew I was missing something.
 
Jason Lehr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do I need to put in the getBet() method? More than just a return of the bet object right?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic