Hi Mike, the first examples in the book are probably only to get to know a bit of the Java syntax and use the compiler and interpreter/jvm. The example you are trying out here seems to be pretty procedural like and not very OOP relevant. For that reason it could be confusing at this time.
I'm trying to help you out here. But you must have a bit of patience with me
Originally posted by Mike Hudek:
Note 1: As I see it here, an instance of the class GuessGame has been created, and it has been given the variable name 'game'.
Yep, a GuessGame variable (which can hold a reference to a GuessGame) is created and then an Object containing the real GuessGame is created (a reference to this object is then assigned to the variable).
Note 2: But here, instances of Player called Playerp1, Playerp2 and Playerp3 are simply declared, without yet having been explicitly defined. This only comes at Note 3:
Yep, we have some variables here, that can hold references to objects. No need to give them a reference to an object, let them point to NULL (no where, i.e. somewhere where it can't hurt anybody). Let them just exist, "they've just come out of factory and haven't been programmed yet".
Note 3: Here three instances of the class Player are created and assigned the names p1, p2 and p3.
Yep, we have now created some real-time objects and want to remember where we put them. Luckily we have variables for this (p1, p2, etc.), so we can assign the handles/references/addresses (whatever) of the new objects to these variables.
I replaced the code in the first few lines of the GuessGame code to the following, and re-compiled it, and everything still seemed to work satisfactorily, so I am not sure why the extra code lines included in the Head First Java text were deemed necessary.
Now you're working really messy. You're right out of OOP. This is procedural programming, which you are doing. You're not thinking "objects".
p1, p2, and p3 are THINGS that the class owns. If you want to DO something with those things (like create objects to put in them), then you need to DO that in a METHOD (if you want to use OOP).
I even don't understand why, under the text book version, there is NOT a compile error, as it would seem to me that the statements
are being included before new instances have been created.
As mentioned: the class owns variables that can point to Players (these are THINGS = member data). When these Players need to be filled with references to objects (somebody needs to do this = the class lets a method do this (method = member function) ).
A class is an "idea" of an object. Try to imagine a horse in your head. What you are imagining is the beginning of a class. You know a horse has 4 legs, a head, and a tail. It can trot, sleep, eat, and make a sound.
But if you wanted to "make" or "find" a horse you would have to leave your head/imagination (even house) and look for a horse somewhere. Once you've found one, you would have something "real" to fill the horse variable "leg", "head" and "tail" with. Going out to look would be one of your class methods. "leg" would be a class member data.
OK, I hope this was helpful
