• Post Reply Bookmark Topic Watch Topic
  • New Topic

question from HFJ  RSS feed

 
Mark Bohn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 7 of HFJ the description of "What goes in a class?" says that a class has one or more methods.

In the "Be the Compiler" exercise on page 63 we have code sample A that starts like this:

class Books{
String title;
String author;
}

On page 68 the solution to this exercise does not point out any problem with this but it seems to me the Books class has no methods at all.

What am I missing?
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a class can have no method
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it will provide no compilation error
 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this (and solve compilation errors if they are there):



Do you get something?
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString() is derived by all the classes from the Object class...

To get a meaningful result your Books class must have implemented the toString() method in it......

Even if you have not implemented the toString() method it will give some value but it will never provide compilation error..........
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are all sorts of things you can say about the exercises in HFJ, and all sorts of ways you could change it, but it looks like something which will compile.

There is actually maybe iffy design in that all fields (except those used as constants) ought to be marked private, but for the purposes of the question asked, that bit of code is OK.
 
Bert Bates
author
Sheriff
Posts: 8945
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Mark,

You know, after 6 1/2 years you're the first person I've heard ask that question!

I will say that in general, the approach we take in HF Java is to help our readers get a solid, basic Java foundation. With that goal in mind we often leave out explanations of unusual or atypical cases. Our opinion is that usually when technical documentation worries about *covering* every possible exception to every possible rule, the documentation gets heavy, dry, and generally hard to read and understand.

I guess you could say that it boils down to a trust issue, we're asking our readers to trust that we're going to teach the most important principles, and that if we leave out a few details it's no cause for panic

As you go through the book you'll also find cases where we return to a particular topic and look at it more closely than on the first pass. Often, during the second or third look we'll say something that is, if you want to get really precise, in conflict with something we said earlier. Again, we do this because we don't want to confuse readers with too many details in the beginning.

hth, and we hope you enjoy the book,

Bert
 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrasannaKumar Sathiyanantham wrote:toString() is derived by all the classes from the Object class...

To get a meaningful result your Books class must have implemented the toString() method in it......

Even if you have not implemented the toString() method it will give some value but it will never provide compilation error..........


Yes, that was my point, you can have a class without methods, but as all the classes inherit from something, and they do it finally from Object they inherit their methods also, so maybe you don't declare a method for the class but that class still has methods. Even if you don't want them :P
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!