• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Dont understand the guessGame (Head First Java)

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, both of you
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic