• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Java Bean naming standard

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am studying for the SCJP exam by reading "SCJP Sun Certified Programmer for Java 6 Study Guide". Here there are a few lines about the Java Bean naming standards. Here in one of the tests there is written that 'add' (prefix) can be used only with Listeners methods. This has been an issue for me even before starting to study for this exam and therefore I thought of asking it now.

The question is this; if you have a class, and this class has an instance variable of type List, then should the public method responsible to add elements to this List be called addXXX(), or getXXX(). For example, I have a class of type Tree that can have one-or-more instances of type Apple. The object Tree stores the Apple objects in a List. Should the object of type Tree make the List available to the outside world through the method; or through the method
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I don't have fresh on my mind the naming conventions of Java Beans I will let someone else to answer that for you.

What I can tell you with confidence is that you compare apples with oranges:)

The first method is is a getter, that is it returns the list to the caller. The second method is a setter, which adds to the contents of the existing list.. I guess both are quite useful on their own and the one definitely doesn't replace the other!

I am sorry if I missed the point of the question,
Nik.
 
Simon Joseph Aquilina
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nik Maradina wrote:one definitely doesn't replace the other!


The thing is that I do confuse one with the other. If I want to add an Apple to the Tree then using the first approach I would do something as follows while using the second approach I would do as follows As you can see from the above example both approaches work fine to add an Apple to a Tree. The book seems to indicate that the second approach - addApple(Apple a) - is not correct since the 'add' prefix should only be used for listeners. In other words it works, compiles, but it is not as should. Therefore is it correct to use the 'get' prefix for a List object?
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Joseph Aquilina wrote:As you can see from the above example both approaches work fine to add an Apple to a Tree. The book seems to indicate that the second approach - addApple(Apple a) - is not correct since the 'add' prefix should only be used for listeners. In other words it works, compiles, but not as should.



The JavaBean standard is not a "best practices" naming convention. Its a standard that allows tools to introspect an object, and figure out what to do. In that regard, it has the concept of properties and the concept of listeners -- and it supports a naming convention for those concepts.

Furthermore, the naming convention is more for convenience, most beans with complex property/listeners/method naming requirements, take the extra steps of providing a beaninfo which allows the javabean to specify exactly what name to use for what.


It is generally recommend to follow the standard, just in case, in the future, you want convert the java class into a java bean. If you disagree with the requirements of the standard, either don't follow it, (or if you must, then provide a beaninfo for it).

Henry
 
Simon Joseph Aquilina
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:It is generally recommend to follow the standard, just in case, in the future, you want convert the java class into a java bean. If you disagree with the requirements of the standard


Thanks for the reply. I always tend to stick with the standards which is why I asked this question Therefore if in the exam I am asked the question Which method names follows the JavaBean standard? (pg79, q2) and I am given the option add<Object>(), then, if this Object is not a Listener I should mark it as wrong. In practice I will stick to always using the getXXX() when working with List since I feel this is more in the spirit of the JavaBean standards.
reply
    Bookmark Topic Watch Topic
  • New Topic