• Post Reply Bookmark Topic Watch Topic
  • New Topic

instance/reference variables in Java -- need some clarification re: an example in Head First Java.  RSS feed

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay -- it's been a few year since I've been here. I want to get back into learning Java, and I came across an example in Head First Java that puzzles me about something.

It's on page 38-40, in chapter 2 ("classes and objects"), the Guessing game code example. There are 3 classes in this application: The GuessGame class (whose startgame() methond actually runs the guessing game), the Player.class (which creates the guesses), and the Game Launcher class, which has the main method.

On page 39, the notes next to the GuessGame class has the following code:


There's a note next to the this that says
GuessGame has three instance variables for the three Player objects


right after that is the beginning of the startgame() method:


and next to that is a note that says:
create three Player objects and assign them to the three Player instance variables


I'm missing something here and I don't know what it is, and I'm not sure how to ask it: How can you create an instance variable for the class when it's not defined in the class, and how can the Player objects be assigned to the instance variables? I thought that instance variables were assigned within the class (the randomly chosen integer defined in the player class)? Is there anyone out there who has this book and can explain to me what I'm missing? I thought that p1 is a on object reference variable to control the object, not some attribute about the class (which would be the integer that is randomly chosen as the player's guess. This kind of assignment didn't seem to be explained earlier in the chapter.

Can someone explain this to me? and if i'm not clear enough I apologize. And I think I may have forgotten how to use the quote and code buttons when writing the topic.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:I'm missing something here and I don't know what it is, and I'm not sure how to ask it: How can you create an instance variable for the class when it's not defined in the class...

But it (or they) are defined in the class - in fact, probably fairly close to where they wrote
"public class GuessingGame { ...".
If it's inside those braces, then it's 'in the class'.

...and how can the Player objects be assigned to the instance variables?

Exactly as they've shown you.

I thought that instance variables were assigned within the class (the randomly chosen integer defined in the player class)?

Which would appear to be exactly what they've done.

As to the "randomly chosen integer" bit, I'm not sure what you're on about. So far, all I've seen is 3 variables declared, and 3 new Player objects assigned to them. I think you may be getting ahead of yourself and thinking about how the game is played, when the code you've shown us has nothing to do with that; it's just setting up things that will be needed by the class later on.

If you're still mystified, try clarifying a bit.

Winston
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies -- I have to get up to speed. Let me post the whole Guessing Game code.


Let me give the code for the Player class as well:



And here's the code for the GameLauncher class:



My question has to do with lines 2-4. Why is the declaration of the p1, p2, and p3 variables splint from lines 7-9? Could it have been written simply:



etc, all in the startGame method? The note next to the code calls p1, p2, and p3 instance variables, but I don't quite understand -- I thought an instance variable was a variable defined inside the class, like the name of a dog in a Dog class, or the color of a button in a Button class. Wouldn't p1 be called the object reference variable; Am I any clearer?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:My question has to do with lines 2-4. Why is the declaration of the p1, p2, and p3 variables splint from lines 7-9? Could it have been written simply...

Answer: yes.

But then the variables would be initialized only when a GuessingGame object is created. If that's the way it should be then sure, you could do it that way; but the way they've done it is also fine. And it will also initialize the Players if the game is restarted (ie, startGame() is run again).

The note next to the code calls p1, p2, and p3 instance variables, but I don't quite understand -- I thought an instance variable was a variable defined inside the class, like the name of a dog in a Dog class, or the color of a button in a Button class. Wouldn't p1 be called the object reference variable; Am I any clearer?

Not sure. As I said above, those variables ARE defined inside the class, so they are instance variables. The only way they wouldn't be was if they had the qualifier static on them.

Or were you thinking they should be defined inside the startGame() method?

Winston
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay -- I think i'm more confused, but maybe not. And I apologize in advance if I'm asking what should be obvious (but obviously not to me )

Are you saying that they're instance variables of the GuessGame class or the Player class?

I think that yes, I should expect to see them declared in the startGame() method; but if so, how would it be different, if at all, when the application were run? And I apologize if I'm missing the obvious, but I'm not a professional programmer.

Did I choose the wrong book to learn Java? Head First was recommended to me over Java for Dummies.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:
I think that yes, I should expect to see them declared in the startGame() method; but if so, how would it be different, if at all, when the application were run? And I apologize if I'm missing the obvious, but I'm not a professional programmer.


If the variables are declared in the startGame() method, then they are *not* instance variables. They will be local variables, and they will have a scope for only the lifetime of the method call.

Henry
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're getting confused by your terms. An instance variable is any variable declared inside a class but outside of any method of that class. Its scope is limited to one instance. The intent for calling this kind of variable an "instance" variable is so that you have an idea of the variable's *scope*. An object reference variable gives you an idea of the *kind* of thing that is being referenced. An instance variable can also be an object reference, just as you can be a student at a school and at the same time be a driver of a vehicle. The names are from different perspectives. If you need to understand each, you should have contrasts from the same perspectives:

instance variables vs: local (scope is only within a method or block), static (scope is for the class, including any instances of the class)

object references vs: primitives (refer to primitive values kept in memory rather than objects)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:Are you saying that they're instance variables of the GuessGame class or the Player class?

I think Junilu's covered most of the terminology stuff but, in answer to your question above:

Where are they declared? In the GuessingGame class, or the Player class?

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!