aspose file tools*
The moose likes Beginning Java and the fly likes why can't I access my variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why can Watch "why can New topic
Author

why can't I access my variable?

Martin Platt
Greenhorn

Joined: Aug 23, 2004
Posts: 9
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

Joined: Dec 10, 2001
Posts: 7023
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
Martin Platt
Greenhorn

Joined: Aug 23, 2004
Posts: 9
Thanks for your help, guys. And sorry about the username - I have updated!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Thanks, Martin!

I hope to see you 'round the Ranch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why can't I access my variable?