Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why can't I access my variable?

 
Martin Platt
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm hoping someone can help me. This is my first attempt at writing a Java game (bat_and_ball) and I've got stuck.

It sounds like a simple problem but I've tried everything I can think of and still no joy.

I need my ball to look up the Y co-ordinate (called z) of the bat to see whether it stays in play or goes out. However, when my ball looks up z with using bat.z I get the error "cannot resolve symbol - symbol:variable bat - location class Ball". Here is the important part of the code:



When it compiles bat.z it says "cannot resolve symbol - symbol:variable bat - location class Ball"

But I know that the object bat exists and has a z variable. Here is the code that defines the bat:



And here is the main class that runs the applet:



Can anyone point me in the right direction? Why can't I access the variable z?

[ August 24, 2004: Message edited by: Martin Platt ]
[ August 24, 2004: Message edited by: Martin Platt ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pine apple,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Also, when posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.

Note that you have the ability to edit your own posts. Just click the icon above your post that looks a bit like a piece of paper.

Thanks Pardner! Hope to see you 'round the Ranch!
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code:


You need to pass in the bat reference either to the constructor or methods.

Example 1:


Example 2:


Joyce
[ August 23, 2004: Message edited by: Joyce Lee ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your question has been answered above, but I'd like to add a few comments. First of all, class names should begin with upper case, so "class player" should be "class Player". This isn't required, but it is good to follow conventions like this when you ask questions. It also helps when you come back to your code at a later time.

For more information about Java coding conventions, take a look at Coding Convetions for the Java Programming Language.

p.s. The above shows you an example of how to fix your code, but it isn't the only solution. I suggest you do a search on this board (or the web) for something like "variable scope". This is the general topic that describes when a variable can be accessed (or not).
[ August 23, 2004: Message edited by: Layne Lund ]
 
Martin Platt
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help, guys. And sorry about the username - I have updated!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Martin!

I hope to see you 'round the Ranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic