aspose file tools*
The moose likes Beginning Java and the fly likes K&B book: problems understanding code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "K&B book: problems understanding code" Watch "K&B book: problems understanding code" New topic
Author

K&B book: problems understanding code

Martin hill
Greenhorn

Joined: Sep 01, 2006
Posts: 6
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Martin hill
Greenhorn

Joined: Sep 01, 2006
Posts: 6
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B book: problems understanding code
 
Similar Threads
a true or false statement
JavaRanch Exclusive! temporary K&B 5 errata
preparing for SCJP 5
IS-A and HAS-A relationship
Doubt in K&B SCJP 5 Book