File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 148

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