Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dont understand the guessGame (Head First Java)

 
Janis Mittelstaedt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
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?

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 p1.name, p2.name or p3.name to get each person's name.

Does that help?
 
Kirsty Beaton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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é
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, both of you
 
Dimitrios Chatzidimitriou
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic