• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GuessGame Head First Java, I am newbie in coding

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.  
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic