• 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

K&B book: problems understanding code

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic