This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Head First Java Book question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Book question" Watch "Head First Java Book question" New topic

Head First Java Book question

Rob Willmann

Joined: Aug 27, 2011
Posts: 1

Greetings all!

I am totally new to Java, and am working through the "Head First" Java book, 2nd ed.

On p. 39 there's a class code example called GuessGame.

Right off the bat, the code looks like this:

My question is as follows.

There's another class called Player, which creates Player object instances.

the GuessGame class creates the Player Object by calling them via:

p1 = new Player();

Why did we need to put the Player p1 code lines at the top?

Couldn't the Guess game have called them by:



shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 153

Player p1 is given to make p1 as an instance variable of the class
if you give this within a method this becomes a local variable whose scope is limited within
the method.
I agree. Here's the link:
subject: Head First Java Book question
It's not a secret anymore!