Hello, like a ton of others I picked up Head First Java and I was guided here by the book.
Programming experience = little to none
I'm currently in chapter 3 looking at this example:
Sorry I just copy/pasted the code so it will probably look terrible in this post!
I understand 99% of the book up until now and 99% of this code. I understand everything about the creating objects/arrays and variables/reference variables and declaring them etc.. what I am not following is how the program decides to take into account the "bark" method at the bottom of the code to produce " says Ruff!" earlier in the program. Up until now I have been following most of the code throughout the book by reading it from top to bottom and it seemed like everything happens in a chronological order. Is there a hierarchy of sorts that exists where methods in a code are defined first and then implemented into the rest of the code? I skimmed through the first two chapters and didn't really find anything to help me understand if things in code take priority over others. Wow I apologize if my question is ridiculously unclear. Any help would be much appreciated. I don't feel comfortable continuing on in the book with this mental gap.
The question is not very clear, but I will take a shot at it.
A Java method is an example of a "subprogram" common to almost all programming languages. A program is a set of instructions; one of those instructions can be to "call" a subprogram.
When calling a subprogram, execution at the place of the call is suspended, and execution continues with the first statement of the subprogram. This is independent of the order in which the calling code and the called code are defined.
Once the subprogram has finished execution, then execution resumes in the calling code with the statement after the call. (There are various exceptions to this; let's NOT try to deal with them now.)
Now, there is a problem with the code you have posted that I am surprised came out of the book. "dog1" is instantiated with an empty constructor, and there is nothing to set the name in that object. Then "bark()" is invoked on that object, and that method uses the value for name. So I would expect it to say something like "<null> says Ruff!" on its first output line. Perhaps they're illustrating what happens in this case, but it looks odd.
Joined: Oct 24, 2011
Indeed, the output does give "nulls says Ruff!"
The only reason I can imagine they included this was because in the previous couple of pages they were describing what happens when you have a null reference with no object. Perhaps this is just to reinforce that idea.
Perhaps this will make my question more clear: Everything in the code makes sense to me until I get to the loop part. Lines 25-28 are basically saying to make all the Dogs perform the bark method until the loop ends. What is escaping my poor brain is if this chunk of code precedes what is supposed to be the actual output (line 33), how does the entire program as a whole know to use that data (line 33) and apply it to the bark method in all the previous lines.
Or, the output " says Ruff!" is added toward the end of the code. Is there a general rule that still makes that method apply to everything else that has been coded previously (i.e. line 06)?
If there are some major programming fundamentals I am missing, please feel free to point me in the right direction. I don't expect anyone to explain a pot of boiling water when I don't know what fire is.
Joined: May 29, 2005
Well, the answer is at least partially in my previous posting.
'bark' is a method of the class 'dog'. At any place where you have an instance of dog, you can invoke the bark method for that dog.
line 06 does this for dog1, and the output is slightly off because that instance of dog has not had its name set. As I said in the previous post, execution is suspended on line 6, starts on line 33, that finishes the method, and therefore execution resumes on line 7.
Same for the loop. Execution is suspended on line 27, starts on line 33, resumes at 28, and does that each time through the loop.
Does that explain what you're after?
Joined: Oct 24, 2011
This has actually been a big help. As for now it seems like I just need to tell myself, "That's just the way Java works." After all, the book does keep telling me not to get bogged down with details, and that things become more clear in the future.... but it's just hard to take that for granted and plow through text when sometimes it isn't completely clear.
To (try to) close off my own topic, is it safe to put it in these laymen terms?:
Line 06 calls on a bark method which I have not yet coded. As long as that method is properly coded later, Java will find it and use it in the appropriate places it was called on.
Rory Baranyai wrote: . . . Line 06 calls on a bark method which I have not yet coded. As long as that method is properly coded later, Java will find it and use it in the appropriate places it was called on. . . .
Unlike some languages, Java™ has no requirements about order of declarations. So you can write your methods before or after and the “javac” tool will get them all in the correct places.
Within a method, constructor, etc., the order of the individual statements and lines matters. The order of initialiser blocks matters too, so it might be a good idea to restrict initialiser blocks to one per class. But initialiser blocks are a more advanced feature which you can forget about at the moment. Those links are not easy to read, so you can ignore them at the beginner’s stage.
What happens in the System.out.println call is described here. You are passing something which might be an Object, so its value is passed to String.valueOf(x). That calls the argument’s toString() method, if it actually exists, but if the argument points to null, it returns the String "null".
Then the + operator is called, which (behind the scenes) creates a StringBuilder (or something similar) with the value of that String ("null"), and then calls the append() method, then it receives the return value from the StringBuilder’s toString() method.
So what you see is n-u-l-l with the bit about says Ruff added to it.