• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

JavaBeans naming standard Question

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following is a question and answer from SCJP exam study guide by kathy sierra and bert bates..

Which method names follow the JavaBeans standard? (Choose all that apply.)
A. addSize
B. getCust
C. deleteRep
D. isColorado
E. putDimensions

Answer:
B and D use the valid prefixes 'get' and 'is'.
A, C, and E are incorrect because 'add', 'delete' and 'put' are not standard JavaBeans
name prefixes. (Objective 1.4)


I can't understand why addSize is wrong ? As add is a valid prefix as per JavaBeans naming standard? Please give an explantion..?
 
Ranch Hand
Posts: 1710
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raja,

Other options may be tempting but as per JavaBeans standards, method name prefixes should be "set" for setter, "get" for getter and "is" for boolean.



For boolean you can also use get!

Thanks and Regards,
cmbhatt
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mr.Chandra,

ya I totally agree with what you said, but the listener registration and removal methods also follow Javabeans naming standard(i.e. camelCase) right ..??



This is the Paragraph from that topic.......


Second, the JavaBean spec supports events, which allow components to notify
each other when something happens. The event model is often used in GUI
applications when an event like a mouse click is multicast to many other objects
that may have things to do when the mouse click occurs. The objects that receive
the information that an event occurred are called listeners. For the exam, you need to
know that the methods that are used to add or remove listeners from an event must
also follow JavaBean naming standards:
JavaBean Listener Naming Rules
1. Listener method names used to "register" a listener with an event source
must use the prefix add, followed by the listener type. For example,
addActionListener() is a valid name for a method that an event source
will have to allow others to register for Action events.

2. Listener method names used to remove ("unregister") a listener must use
the prefix remove, followed by the listener type (using the same rules as the
registration add method).

3. The type of listener to be added or removed must be passed as the argument
to the method.


For the listner case..

Is it that " add " prefix should be followed by some Listener nameonly



and not


as size is not a listener type.

Please give your suggestions...
[ April 06, 2007: Message edited by: Hardik Raja ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add/remove is definitely for listeners. But addSize is probably not a listener here (just guessing). The authors probably did not think that "add" was used for listeners.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you check the K & B errata link at the top of this page, you will see that this problem is referenced. It should be made clear that addXXXListener and removeXXXListener are the correct conventions. So addSize is incorrect usage.
[ April 06, 2007: Message edited by: Barry Gaunt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raja,

Adding to my previous post...

addXxxListerner()
removerXxxListener()

are valid javabeans conventions for adding and removing listeners.
But as Barry said removeX() or addX() are not JavaBeans naming standards.


One should not miss the K&B errata. It is as vital as your SCJP preparation.
"No use banghead after coming from exam blaming authors"
Thanks and Regards,
cmbhatt
 
Do you pee on your compost? Does this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic