I'm working my way though the SCP&D for
Java 2 book by K&B and I thought I was understanding most of it but all of a sudden my brain has turned to mush.
I'm up to chapter 5 and on page 299 came across this code:
public class Horse extends Animal {
private Halter myHalter;
public void tie(LeadRope rope) {
myHalter.tie(rope);
}
}
pulic class Halter {
public void tie(LeadRope aRope) {
// ..etc
}
}
From what I've read line 2 declares myHalter to be a Halter object, but where does it get created or instantiated and put on the heap? My understanding is that myHalter will contain "null" after line 2 runs, but it would need to contain a pointer to a real object for line 4 to work.
Should line 2 be?:
private Halter myHalter = new Halter();
Also, I realise these are just code snippets, but I wonder why both classes are public forcing them to be kept in separate files. In practice is it more typical to have many files with one or just a few classes per file, or is it better to have fewer files with more classes per file?
Thanks.