To provide good maintenance for Java Programming, Sun has a coding convention so that all programmers will have same code-typing habit. Here are some conventions agreed by Java developers
a) Classes and interfaces : should be in camelCase, 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 capitalized as well.
For classes, the name is typically noun, such as
Horse
DiskStorage
UserName
For interfaces, the name is typically adjectives, such as
Clickable
Runnable
b) Methods : the first letter should be lowercase, in camelCase form, and verb-noun combination, for example
getHeight
kickHard
setUserName
c) Variable: first character should be lowercase, then next characters are in camelCase, and it should be short yet meaningful, such as:
myAccount
boxHeight
d) Constant: in Java, you can use "static" and "final" keyword to creat constant. By convention, constant should be all in UPPERCASE wih underscore for sparation between characters, such as:
BOX_WIDTH
ACCOUNT_NUMBER
Java Beans Property
There are also some convention for JavaBean properties
a) if property is a boolean, the getter method is either "is" or "get" ("is" is preferable) like
isRound()
isStarted()
b) if the property is not boolean, the getter should be "get", such as
getHeight()
getTotalAccount()
c) the setter for both boolean and not boolean should be "set". Like "setMaxHeight" is the correct way to set the value or maxHeight variable.
d) The first character after is, set, or get should be in capital.
e) Setter must posses public signatures and void return type
f) Getter must be public with no argument and matched return type with setter method
There are also some rules for JavaBean Listener Naming mechanism
a) Listeners are used to register a listener and must use prefix "add" followed by listener type. For example:
addMouseClickListener() is a valid method to event source to register.
b) Listeners used to remove event sources, should use "remove" prefix, such as
removeMouseClickListener()
c) the type of added/moved listener must add as arguments. For instance:
public void addMyListener(MyListener m)