Java Code Conventions:
Classes and interfaces The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase (a format that's sometimes called "camelCase"). For classes, the names should typically be nouns.
For example:
Dog
Account
PrintWriter
For interfaces, the names should typically be adjectives like
Runnable
Serializable
Methods The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs.
For example:
getBalance
doCalculation
setCustomerName
Variables Like methods, the camelCase format should be used, starting with a lowercase letter. Sun recommends short, meaningful names, which sounds good to us.
Some examples:
buttonWidth
accountBalance
myString
Constants Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators:
MIN_HEIGHT
JavaBeans Standards
The JavaBeans spec is intended to help Java developers create Java components that can be easily used by other Java developers in a visual Integrated Development Environment (IDE) tool (like Eclipse or NetBeans). As a Java programmer, you want to be able to use components from the Java API, but it would be great if you could also buy the Java component you want from "Beans 'R Us," that software
company down the street.
First, JavaBeans are Java classes that have properties. For our purposes, think of properties as private instance variables. Since they're private, the only way they can be accessed from outside of their class is through methods in the class. The methods that change a property's value are called setter methods, and the methods that retrieve a property's value are called getter methods.
JavaBean Property Naming Rules
■ If the property is not a boolean, the getter method's prefix must be get. For example, getSize()is a valid JavaBeans getter name for a property named "size." Keep in mind that you do not need to have a variable named size (although some IDEs expect it). The name of the property is inferred from the getters and setters, not through any variables in your class. What you return from getSize() is up to you.
■ If the property is a boolean, the getter method's prefix is either get or is. For example, getStopped() or isStopped() are both valid JavaBeans names for a boolean property.
■ The setter method's prefix must be set. For example, setSize() is the valid JavaBean name for a property named size.
■ To complete the name of a getter or setter method, change the first letter of the property name to uppercase, and then append it to the appropriate prefix (get, is, or set).
■ Setter method signatures must be marked public, with a void return type and an argument that represents the property type.
■ Getter method signatures must be marked public, take no arguments, and have a return type that matches the argument type of the setter method for that property.
JavaBean Listener Naming Rules
■ 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.
■ 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).
■ The type of listener to be added or removed must be passed as the argument to the method.
■ Listener method names must end with the word "Listener".
Other Helpful Resources:
Code Conventions:
New to Java:
No comments:
Post a Comment