Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B book: problems understanding code

 
Martin hill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that's the complete class Horse (i.e. there isn't more code that you didn't show us here) then yes, myHalter is not initialized to refer to a Halter object and calling the method tie(...) in class Horse will lead to a NullPointerException.

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?

Yes, it is typical, and better, to have many files with one class per file. That makes it easy to find the source file where a specific class is defined.
[ September 01, 2006: Message edited by: Jesper Young ]
 
Martin hill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper,
that is the complete Horse class; good to have it confirmed that it won't work as is.

I went to the trouble of neatly indenting the above code (using spaces) but somehow the leading spaces got stripped out after I submitted my question. If I try "tab" instead in my browser the text input field just loses focus, so that doesn't work either.

I notice other members get their code indenting correctly, does anyone know how they do it?

Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin hill:
I notice other members get their code indenting correctly, does anyone know how they do it?

Welcome to JavaRanch Martin!

If you scroll down on the page when typing your post, you will see a section called "Instant UBB Code." Click the "code" button. Some tags called CODE will appear in the textbox. Then just copy/paste your code between those tags and indentation will be preserved.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys -

We just finished moving so my books are packed away, but I think you found an existing errata - you might want to check out the errata list.

hth,

Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic