aspose file tools*
The moose likes Beginning Java and the fly likes Is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is "Head First Java" Example Code Outdated/Incorrect?" Watch "Is "Head First Java" Example Code Outdated/Incorrect?" New topic
Author

Is "Head First Java" Example Code Outdated/Incorrect?

Ben Stanley
Greenhorn

Joined: Sep 01, 2010
Posts: 15
Hi everyone,

This is my first post and I am looking forward to spending a lot of time here at the Ranch in the coming months/years!

I've got a question regarding how current/correct the code in the various examples one can find throughout "Head First Java" is. I'm using the second edition (the most recent edition) published in 2005 which is supposed to be current through Java 5.0. I am using JDK 1.6.0_21.

While this book is very helpful, I've had some problems with some of the examples. For example on Pages 39 and 40, one can find code for the "Guessing Game" program. When I tried to compile the code, I got a few compiler errors that Google research revealed were caused by the fact that one is only allowed to have one top-level public class per file. The sample code, by contrast, has three top-level public classes in one file and the book didn't mention anything about the need to have them in separate files (this problem is discussed in greater detail here: http://www.coderanch.com/t/479359/java/java/multiple-classes#2150099 ).

Then, on page 62, is another example that has the following structure (this isn't the actual code):



In addition to not compiling properly, this seems odd because instead of what we've seen up until this point--a class with the main() method on the one hand and a separate class creating each object (i.e., defining object variables and methods, etc.) on the other--this example puts the main method inside of an object class (the dog class, in this case).

All of this has led me to wonder whether or not this code is complete, up-to-date, and accurate. I'm sure it is and that I'm just missing something but I thought I'd at least ask the question. Are these things a product of changes introduced with Java 6? Or is this code for illustrative purposes only and not meant to be typed verbatim by those reading this book?

Thanks in advance,

Ben
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Ben,

Welcome to JavaRanch!

Short answer: none of this has anything to do with any changes in Java 6. It's just that Head First doesn't (always) intend you to run off and type things in as they appear in the book and try to run them. Many of the examples are intended as illustrations only. None of them are out of date, though.

The thing about the three classes in one file: I don't have the book handy, but I doubt it specifically suggests that you put the three classes into one file and try to compile them: it probably just shows the three classes together, yes? It's the reader who decides to create a file containing them.

Regarding the Dog: there's no reason why "main()" has to be in a class by itself. It's often a good practice, and in large projects it's universally the case, but for very small programs it's really just an artifice, and no harm is done by forgetting it now and then. This is just a "don't worry about it" situation.

There's one example early in the book that folks very often type in and try to run, only to get an error message about a missing main() function, and they come here to ask about it. The example is simply not meant to show a complete program, and you're not intended to type it in and run it -- but people try anyway.


[Jess in Action][AskingGoodQuestions]
deepak kushwaha
Ranch Hand

Joined: Jul 17, 2010
Posts: 33
The book is really really good. As far as your question is concerned they have done that deliberately to make you learn and to use your mind. read the intro page no. xxix under the topic "the code examples are as lean as possible". You will understand.
Ben Stanley
Greenhorn

Joined: Sep 01, 2010
Posts: 15
Thanks to you both for the speedy and thorough replies and for the kind welcome!

You are correct, Ernest, that there is no explicit suggestion to put everything in one file and compile it. I just thought it was implied since it didn't say otherwise and everything was next to each other on the pages.

I agree too, deepak, that this is a very good book. I'm really enjoying it so far.

Thank you both!
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Ben Stanley wrote:Thanks to you both for the speedy and thorough replies and for the kind welcome!

You are correct, Ernest, that there is no explicit suggestion to put everything in one file and compile it. I just thought it was implied since it didn't say otherwise and everything was next to each other on the pages.

I agree too, deepak, that this is a very good book. I'm really enjoying it so far.

Thank you both!


A good practice I got into when reading the book was typing and compiling the code in the book myself. If it didn't compile/wouldn't run, I'd figure out why and modify the code to make it functional. It was just a quick little exercise each time, but I got to write code, learn syntax, do troubleshooting, and generate my own code.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Ben Stanley
Greenhorn

Joined: Sep 01, 2010
Posts: 15
Excellent idea! I think I'll try to do the same. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is "Head First Java" Example Code Outdated/Incorrect?