In addition to Java Code Conventions on naming standard, JavaBeans provide another important naming standard that should be complied as well by developers.
This article discusses only the naming aspect of JavaBeans specification. However, a brief explanation of JavaBeans is also provided below.
What is JavaBeans?
JavaBean specifications are intended for ease of sharing Java components, visual or non-visual, among developers to use with visual Integrated Development Environment (IDE) tools such as Eclipse and NetBeans. A source code that adheres to JavaBeans specifications can be recognized nicely by an IDE and then can be used and reused by any different programs that need it. In another word, JavaBeans is an aspect of object reusability that’s implemented in Java programming world.
Now, often in real programming activities using an IDE tool, you will need another java component to help the existing components achieving their task but you couldn’t find the component you want in Java API nor you have enough time to write the component’s code yourself right from the scratch. So what you would do is to find that component in other resources, probably from other components collections of yours or to call other developers or software companies down the street, and see if they have the component you want and are willing to share it or sell it to you. If you’re able to find it, then would it be great if you don’t have to modify the component again from scratch in order to integrate it in to your program, but instead you could have IDE tools recognize the component perfectly and use it directly to your program without any modification. Well, JavaBeans is here to help. You will be able to do that only if the component you find from other sources, including from your personal resources adheres to the JavaBeans specifications.
Wouldn’t that make your life easier than ever…? And all you need to do is to conform to the Java Code Conventions along with JavaBeans specifications whenever you code.
JavaBeans Specification for Naming Standard
Classes that conform to JavaBeans specifications are called Beans. They have properties, i.e. instance variables. A good programming practice is to keep every instance variables private whenever possible and provide methods to access and to modify them from the outside world. The method that can access them is called getter method and the method that can modify their value is called setter method.
Now, JavaBeans specs require the getters and setters to be named as simple as follows:
1. The getter should be named as getX() for property X. If the property is a boolean, then the getter should be named either as getX()or isX().
getDiameter() for property “diameter”
getAuthenticated() or isAuthenticated() for property “authenticated”.
2. The getter should be marked public, returns the type of the corresponding property, and takes no arguments.
So, taking the same example as previous, the getter declaration would be:
public long getDiameter() for property “diameter” which is of type long.
public bool getAuthenticated()for property “authenticated” which is of type bool.
3. The setter should be named as setX() for property X.
setDiameter() for property “diameter”.
setAuthenticated() for property “authenticated”.
4. The setter should be marked public, return a void type, and take an argument of the type of the corresponding property.
So, taking the same example as previous points, the setter declaration would be:
public void setDiameter(long newDiameter) for property “diameter” which is of type long
public void setAuthenticated(bool authenticationPolicy) for property “authenticated” which is of type bool
JavaBeans specs also cover other methods that support event-handling process, a process to respond the event generated as a result of interaction between the program GUI and users. That response is provided by a class who implements a java.awt.EventListener subinterface, e.g. ActionListener, MouseListener, and ChangeListener. For example, mouse listener will define the response of the program to a mouse-clicked event as a result of interaction between the program and the user through a mouse-click, that’s when users click a GUI component of the program.
If a java class needs to support a particular event handling, then it needs a method to register or remove an event listener to or from an object of that class. JavaBeans requires that method to be named as follows:
1. A method used to register an event listener should be named as addXxxListener() and take an argument of type XxxListener
addMouseListener(MouseListner ml) which register a mouse listener which in turn will respond to a mouse event.
2. A method used to remove an event listener should be named as removeXxxListener() and take an argument of type XxxListener
removeMouseListener(MouseListener ml) which removes a particular mouse listener.
That’s all….a brief article about JavaBeans specifications on naming conventions.
JavaBeans specifies a lot more of things. To learn more about it, one of the best places to go is Sun online tutorial about JavaBeans: http://java.sun.com/docs/books/tutorial/javabeans/index.html