• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ben Stanley
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Stanley
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent idea! I think I'll try to do the same. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic