This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java Book question

 
Rob Willmann
Greenhorn
Posts: 1
Debian MySQL Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

?

Thanks!

rob
 
shivang sarawagi
Ranch Hand
Posts: 158
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic