• Post Reply Bookmark Topic Watch Topic
  • New Topic

GuessGame Head First Java, I am newbie in coding  RSS feed

 
Dama Adhikara
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am pretty new in this coding stuff,
I never learn any programming language before.
So I think I am gonna need your help and guidance to stretch out bit more explanation from Head Java First book.

In time I am on chap 2, so far I can understand what I have red clearly but got stuck now on
digesting the GuessGame case,lol

So here it is, here the GuessGame Code from the book:


This code run excellent but I am just curious to understanding line by line, so here are my questions:
1. line 8,9,10,62 why there is a 0 value? I mean if I delete the 0 value it still runs perfectly just the same, does the 0 value mean counting each loop?
2. line 8,9,10 also if I delete the 0 value and put them before the line 7 (above: public void gameStart) it also works the same, so what is the difference with that treatment? because I feel its much more neat to put it before line 7 just like declaring player instance variable.
3. this one is unrelated question to the case, can someone give me more bold and easier explanation to public, static meaning?

anyway this how I modify the code and results the same:
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think the idea of the game is that you guess a number and see whether it is the same as the computer would have guessed. I think guessp1 is one of the numbers you are supposed to guess.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. line 8,9,10,62 why there is a 0 value? I mean if I delete the 0 value it still runs perfectly just the same, does the 0 value mean counting each loop? 

When a variable is declared outside of a method, it is automatically initialized to its default value.  For int, the default value is 0.  It also has a different scope, that is, it can be seen in other methods. 

When a variable is declared inside a method it is called a local variable and it must be initialized.  The scope is restricted to the method it's declared in.  This is a good thing.  In general, you should declare a variable "as close to" where it is used as possible.  You should always prefer local variables to others (instance and class variables) so even though your modified program works, it is not recommended.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone give me more bold and easier explanation to public, static meaning? 

I'll try.  "Public" is an access modifier.  It declares that a variable, method, or class can be accessed from code in other files and packages.

"Static" means that the variable or method belongs to the class, not instances of the class (that is, objects).  A variable or method that is not static must be accesses by an instance of the class whereas a static variable or method is accessed by the class name.

More bold and easier?  I don't know.  
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!