• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

returning a value in my program

 
Ranch Hand
Posts: 424
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: 424
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
 
Bartender
Posts: 5904
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 424
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
Bartender
Posts: 5904
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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?
 
Bartender
Posts: 3323
86
  • 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.

 
Marshal
Posts: 64666
225
  • 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: 424
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?
 
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • 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: 424
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: 424
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: 424
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: 64666
225
  • 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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!