This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android 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: 147

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
Similar Threads
Another beginner question
Do I need 1 of 3 seperate .java files???
Really, really new to Java. Trying to compile small program, help! cannot resolve symbol
Head First Java Guessing Game