wood burning stoves
The moose likes Beginning Java and the fly likes confused newbie needs help! simple Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "confused newbie needs help! simple" Watch "confused newbie needs help! simple" New topic

confused newbie needs help! simple

Mike Smike
Ranch Hand

Joined: Aug 25, 2004
Posts: 33
Hi guys,

I'm trying to write a program, but am confused a bit about referencing objects and methods in different classes etc.

My problem is this. In the following code I get an error:

The error is as follows:

There is 1 error for each statement. Now is declared in the PlayerDetails class in the following way:

The update method is in the main class - the WarGame class, and the detailsField objects are in the PlayerDetails class. So how can I get the call to work in the WarGame class?
I tried:


but both give me errors.

Is there something wrong with my fundamental understanding of how classes work? Please help me

Thanks loads buddies!

Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Hi Mike,

It seems you need an instance of the PlayerDetails class (i.e. a PlayerDetails object) in order to call methods on it (unless it's static that is, but I won't go int0 that here). So for example you need to think more along the lines of...

I think, instead of...

SCJP 1.4, www.gsi3d.org.uk
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Well, the key is exactly what you pointed out: detailsField belongs to the PlayerDetail class, so it isn't available in the WarGame class.

First of all, the problem with

is that this is the syntax used to access a static variable. You can easily get this to work by adding the keyword "static" to the detailsField declaration:

However, this is not a very good Object-Oriented approach to the problem. A better declaration is this:

However, you need to understand that without the static keyword, you need to create an object from the PlayerDetails class, just like Ben explained. But if you declared it as private like I show above (and you should!), then Ben's solution will still give you a compiler error. One solution is to add a method to PlayerDetails that looks something like this:

(Sorry, I don't know the type used for the variable troops. Please fill in the apprpriate class name or primitive type.)



Java API Documentation
The Java Tutorial
Mike Smike
Ranch Hand

Joined: Aug 25, 2004
Posts: 33
Wow - thanks guys - that was really helpful! And quick response time too - this forum is great.

Layne - why should I declare the detailsField JTextField as private? I know that stops other classes chaging the value of it or something but I'm not quite sure. I havn't use 'private' once in my whole code - is that something to worry about? I usually just use the default access control.

Thanks again!

Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342

Declaring variables as private and then providing accessor and mutator (getter and setter) methods for them is a practice known as encapsulation and is an important part of object oriented programming. Although you are not required to use it and your code will work without it it is a good practice to get into.

The idea behind it, as I see it, is that by preventing unrestricted outside access (i.e. from other classes) to the variables in the class you can do things like checking for erroneuous values. It provides a clean interface to your class, making it easier to re-use and the design more modular and your classes more robust. Here's an example...

A Person class has a variable called age which is an int. If you allow default or public access to this variable, some code in another class could legitimately do this...

which obviously doesn't make much sense in most cases! So, in Person.java you could do something like this...

then in other classes you would write something like...

hope this helps a little! I know some of this stuff is confusing when you start out, but trust me it does make sense after a while
I agree. Here's the link: http://aspose.com/file-tools
subject: confused newbie needs help! simple
It's not a secret anymore!