We are currently having same tests for the OCA certificate and today we run into a minor an issue. I'll try to keep is short:
Which are methods using JavaBeans naming conventions for accessors and mutators?
(Choose all that apply)
A. public boolean getCanSwim() { return canSwim;}
B. public boolean canSwim() { return numberWings;}
C. public int getNumWings() { return numberWings;}
D. public int numWings() { return numberWings;}
E. public void setCanSwim(boolean b) { canSwim = b;}
The answers (as specified by the OCA SE 8) : C and E
Our discussion was on the point C:
public int getNumWings() { return numberWings;}
The point of my colleagues was that it is wrong due to the rule mentioned below. The method accessor must've been getNumberWings so the point C was wrong. I have attached the table of rules from the OCA, where it think the rule 5 is wrong. PHOTO of Rules for JavaBeans naming convention on OCA SE 8 page 206
From my knowledge the name of the method doesn't have to respect the the property. What are your thoughts on this?
OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808
I tried to find a proper answer also on:
JavaBeans conventions from oracle: http://download.oracle.com/otn-pub/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/beans.101.pdf?AuthParam=1484818426_7e07f5a35c14ebfbadb2c68798198d7e
JavaBeans Conventions (Java in a Nutshell)
JavaBeans are classes that encapsulate many objects into a single object (the bean). It is a java class that should follow following conventions: Must implement Serializable. It should have a public no-arg constructor.
JavaBeans provide default constructor without any conditions or arguments. JavaBeans are serializable and are capable of implementing the Serializable interface. JavaBeans usually have several 'getter' and 'setter' methods. JavaBeans can have several properties that can be read or written.
The getter should start with 'get', followed by the member name, with its first letter capitalized. Also the latest conventions I heard of, say that we should avoid multiple capital letters one after another. For example getHTMLtooltip is wrong. it should be getHtmlTooltip instead.
A JavaBean is a Java object that satisfies certain programming conventions: The JavaBean class must implement either Serializable or Externalizable. The JavaBean class must have a no-arg constructor. All JavaBean properties must have public setter and getter methods.
Your colleague is (I think) arguing that
public int getNumWings() { return numberWings;}
violates the JavaBean because the field name and the property name are different.
That is not supported by the spec. The Java beans conventions (as codified here) state:
6.2.2. Properties
A bean defines a property
p
of typeT
if it has accessor methods that follow these patterns (ifT
is boolean, a special form of getter method is allowed):Getter
public T getP()
Boolean getter
public boolean isP()
Setter
public void setP(T)
Exceptions
Property accessor methods can throw any type of checked or unchecked exceptions
Note that it does not say anything about the name of the (typically) private
field that holds the value of the property. Indeed, the field may not even exist ... if the property value can be represented some other way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With