Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misunderstanding on JavaBeans naming convention for methods on OCA

Tags:

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)

like image 473
Iacobescu Radu Avatar asked Jan 19 '17 10:01

Iacobescu Radu


People also ask

Which convention is followed by JavaBeans?

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.

Why should I use JavaBeans?

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.

Which of the following getter methods is written correctly as per the Java naming rules?

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.

What is JavaBeans specification?

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.


1 Answers

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 type T if it has accessor methods that follow these patterns (if T 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.

like image 105
Stephen C Avatar answered Sep 23 '22 10:09

Stephen C