Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrong answer in K&B SJCP 5 self test 1.2?

 
Saskia de Jong
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry if this has been reported before or that I've overlooked something, but the answer to self test 1.2 in the K&B SJCP 5 book seems wrong to me.

The questions asks for compilable code. Answer B is given as:



The book says answer B is a correct answer, but unless {} is to be read as "any legal implementation", this answer is wrong. The code can't compile since there's nothing being returned.

The following would surely compile:


The following would compile if Bark had a public no-arg ctor (but we don't know):


But the code as given does absolutely not compile. Did I just miss the definition of {} for quesions somewhere?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Saskia,

You've found a big honking, gravel-hauling errata...yikes! I'm really sorry, I don't know what happened there but something got WAY out of whack with that question!

Well we posting an errata list very soon, and when we do we'll post the link in this forum.

Thanks for your help,

Bert
 
Saskia de Jong
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bert Bates:

Thanks for your help,
Bert


You're welcome. At least thanks for the quick reply and for clearing this up

Btw, another question that I have doubts about is 1.5. It asks for valid method names according to the Javabeans standard. The answer is that 'addSize' is not correct, since 'add' is not a standard name prefix. However, 'add' certainly is a standard prefix for event listener registration.

Both the Javabean spec (section 6.5 page 29) and your book (page 9) says "add" + eventlistener type:



Now I'll agree that Size would be a strange name for an eventlistener, but I haven't seen it written anywhere that there's also a naming rule for the listener type itself. Specifically, your book makes no mention of the fact that the type names MUST end with "Listener". Therefore, when taking the self test one must assume "addSize" is a valid name.

Could you also clearify this issue?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Saskia,

I think this one isn't an errata, although I might add a sentence in the book to be a little more explicit. In section 8.4 of the JavaBean spec it states that Listener methods have to end with the literal "Listener".

hth,

Bert
 
Saskia de Jong
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Thanks a lot Bert!

For other people who are interested in this matter, this is what the spec exactly says in 8.4:




where both methods take the same �<EventListenerType>� type argument, where the �<EventListenerType>� type extends the �java.util.EventListener� interface, where the first method starts with �add�, the second method starts with �remove�, and where the �<EventListenerType>� type name ends with �Listener�.


(emphasis added by me)
[ February 11, 2006: Message edited by: Saskia de Jong ]
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Saskia and Bert. Good information. I think there's another thread around here somewhere where they were asking pretty much the same question.

I struggled when I saw that question too, but not because of the Listener bit. I just know how common it is to have addBlah and removeBlah in the real world when you have collections of Blahs as an instance variable for a class. I just assumed that was because it was part of the Java Beans spec. After missing the question I went and read it and found the stuff that you're referring to. It's funny the things you pick up as "standards" just because you see them so often. This is making me look closer at some of these things because Java Beans standards mean a lot in the real world since a lot of tools and infrastructures are using them with reflection for working with things. Good stuff to know.

Again, thanks,
Josh
[ February 11, 2006: Message edited by: Joshua Smith ]
 
Timothy E. Hay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

regarding the other thread discussing Listener naming conventions, that was one of mine titled, imaginatively: "JavaBeans Listener naming conventions?" on Jan 31st, 2006.
But what Bert and Saskia have said above reiterates those comments - ie: by quoting from Section 8 in the JavaBeans API spec.

Cheers,

Tim
[ February 11, 2006: Message edited by: Timothy E. Hay ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic