• Post Reply Bookmark Topic Watch Topic
  • New Topic

returning a value in my program  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

returning a value of a player bet to my main method...its working but i have a few questions.

here is my main class:



and here is my other class with a few methods:



so...as i said it is working...

but this line:

System.out.println("bet placed =€" + gameIntro.placeBet());

how come the method runs and returns a value,followed by the print statement that prints the return...
im guessing in the statement java gives methods precedence over the print function? is this right?

please feel free to criticize the code,and tell me if im going in the right direction.
 
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh i forgot...

should private int moneyPlaced;

be a class variable so that other classes will be able to see this variable?

thanks for reading
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:

how come the method runs and returns a value,followed by the print statement that prints the return...
im guessing in the statement java gives methods precedence over the print function? is this right?

More precisely,  evaluating the the '+' operator happens from left to right. When a method is encountered it must be called in order to have an operand for the '+' to have something to operate on.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,is this socially acceptable or is there a better way of doing this?
can it lead to problems etc?...is it bad practice?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:thanks,is this socially acceptable or is there a better way of doing this?
can it lead to problems etc?...is it bad practice?

I don't understand this. Do you mean "socially acceptable" to ask questions?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Jon means is this good programming practice.

In the type of example you have shown providing the method call has no side effects (and it's probably a design problem if it does) then yes this is fine. If however you are concatenating several values from different method calls and the code starts becoming difficult to read then it may be better to store the results of the method calls in sensibly named local variables and concatenate them.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:oh i forgot...

should private int moneyPlaced;

be a class variable so that other classes will be able to see this variable?

thanks for reading
That question does not make sense. A private field is not visible outwith its class, but it is visible in all instances of that class. It makes no difference to visibility whether the field is instance or static.

I have a simple rule of thumb to explain whether something shou‍ld be static:-
Somebody else wrote:Shou‍ld my variable be static?
Campbell wrote:Have you got a good explanation for making it static?
Somebody else wrote:Well, I think maybe ....
Campbell wrote:No.
I you do not have a good explanation for making something static, then it shou‍ld not be static.
.
Keep the field as private instance (=non‑static). If access outwith the class is required, write a getXXX() method. A field shou‍ld not be visible to other classes.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guy,

the one thing im not able to do is see my playerMoney variable in my main method...so i cant minus the bet placed from the players money
is this an access control problem? any ideas?
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:hey guy,

the one thing im not able to do is see my playerMoney variable in my main method...so i cant minus the bet placed from the players money
is this an access control problem? any ideas?
Show us the code you're using.

If the code is the one you posted earlier, question is, why you have maked it (playerMoney) as static in the first place?
I don't see any constructor in your DatrCore (strange name though) class, while most likely there should be one. However why playerMoney is in that class? Shouldn't it be in Player class?

Please tell us more about your project, so we could see more of a picture.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Liutauras Vilda

i thought static variables were loaded into memory and were always available from every class...could have sworn that was what i read...anyways
i wont really need a player class
just the main
core (which just handles the menus etc)
and horses (which will create the horses and race them)
then the player either wins or loses money
but where should playerMoney be then? as to make it changeable from the horse class and main.

thanks for replying
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it working

i was trying to access the static variable in my other class in my main method using the class name duh!!!

its the object name i created i have to use

so
gameIntro.playerMoney works fine

it really is a head job to get used to this stuff!!!

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lastly forgot to ask,

even though it is a tiny program

is it more OOP by design...am i on the right track?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . . is it more OOP by design...am i on the right track?
Show us where you are creating objects in that code. Is your current code different from what you first posted?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!