File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Dont understand the guessGame (Head First Java) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dont understand the guessGame (Head First Java)" Watch "Dont understand the guessGame (Head First Java)" New topic

Dont understand the guessGame (Head First Java)

Janis Mittelstaedt

Joined: Dec 01, 2010
Posts: 12
I've began learning Java yesterday with the book Head First Java.
Now im at the GuessGame Example and I dont fully understand it...

First of all here is the code:

What i dont understand is:

I know these are instance variables, but i do not know what instance variables are

I've tried to find out and all I know is that they represent the state of the object. I dont really understand what that is (maybe because, as you may already have noticed, english isnt my primary language)

So could someone explain how they work/what they are for?

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11923

if you were going to have a real game with real people, you could make three nametags that say "Player1", "Player2" and "Player3". When your guests arrive, you would assign a nametag to a specific person.

That's really all we're doing here.

Lines 2-4 in your code is you creating the nametags with those labels - but they don't represent a real thing yet. We just know we (might) use them later.

Then, on lines 7-9, you create a Player object, and assign the nametag to them, one at a time.

An instance variable means that whenever the object is created, you will have a variable called that. So, somewhere, there is code that creates a Player. That class might have an instance variable of type String, that holds a name. You could then refer to, or to get each person's name.

Does that help?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kirsty Beaton

Joined: Dec 06, 2010
Posts: 9
As someone that is also new to Java, would it be correct to say that lines 2-4 are allocating space for Players? And lines 7-9 are filling that space?
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Kirsty Beaton wrote:As someone that is also new to Java, would it be correct to say that lines 2-4 are allocating space for Players? And lines 7-9 are filling that space?

No. Line 2-4 are declaring some variables. line 7-9 are instantiating some objects, and assigning these objects to line 2-4 variables.

[My Blog]
All roads lead to JavaRanch
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11923

I personally think of it as

lines 2-4 are putting blank address cards in a rolodex - but I'm in my mid forties. Younger people might think of it as putting three people's names in your phone - but nothing else.

Lines 7-9 are the construction workers actually building the house, telling us what the address is, then me writing it down on the cards/you entering the actual address in your phone.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45358
Welcome to JavaRanch, both of you
Dimitrios Chatzidimitriou

Joined: Aug 21, 2010
Posts: 14
Hello! Of course I am really newbie in java.. and I am not sure if I have the right book to start with (Head First java).

What I would like to ask is if the lines 2-4 could be written right after the 6th line.

Also I am not sure how I should think before I start writing my programs..

What I mean is how should I know that for the above Guess Game I need to have 3 classes, Player.class, GuessGame.class and GameLauncher.class ?

There is no way to write all this game in a single one .class ?

Thank you!
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Java is object-oriented, so you need to start thinking about the objects involved.

Of course you could easily write it as one class if you wanted to, but writing it as 3 is easier and more flexible, when you get used to it.

Lines 2-4 that you mention declare the Player variables. In this case, it would work if you declared them in the startGame method, but only because there's only this method in the class. The Players are instance variables belonging to any instance of the GuessGame class. If you declared it within the method, they would be local variables to that method.
I agree. Here's the link:
subject: Dont understand the guessGame (Head First Java)
jQuery in Action, 3rd edition