Identifiers and JavaBeans
In Java, we will be creating lots of classes with variables and methods. As such, we need to be able to identify each one to be able to use them, same as us having names to call each other. We call these names as identifiers and Java has rules on creating them.
Legal Identifiers. Legal identifiers are identifiers which comply to rules in creating identifiers set by Java.
Rules for Legal Identifiers:
- Must start ONLY with a letter, a dollar sign ($), or an underscore (_).
- After the first character, the identifier can contain any combination of letters, numbers, dollar signs and underscores.
- Must not be one of the Java keywords.
- Identifiers are case-sensitive. So
bar
is different fromBar
(Not exactly a rule but you need to remember this when you are using identifiers)
int buzz;Examples of ILLEGAL identifiers:
int $name;
int _AgE;
int $$_$;
int an$unnecessarily_long$identifier_;
double 8uZz;
double !add;
double e-mail;
double %xyz;
double Abc def;
Keywords. Keywords are reserved words by Java. These are primarily used as syntax in Java. Below is a table of Java keywords. Don't worry if you are not yet familiar with some of them we will tackle them as we go along. Click here to see the table.
Java Code Convention. Using a set of code standards helps in reducing effort in testing, maintaining and enhancing existing codes.
Remember that even if an identifier does not follow the naming convention if it adheres to the rules of a legal identifier your code will still compile.
Classes and Interfaces. First letter should be capitalized and if the identifier is composed of several words then the first letter of each subsequent word should be capitalized as well.
Class names should be nouns.
Examples:
PersonInterface names should be adjectives.
DepartmentHead
MyPetStore
Examples:
SerializableMethods. Should follow camelCase convention. First letter of first word is lowercase and the first letter of the succeeding words should be capitalized. Method names typically are verb-noun compounds.
Comparable
Runnable
Examples:
drawShapeVariables. Should follow camelCase convention as well. Variable names are usually nouns. Make them short but descriptive.
createPlayer
interpolateValue
Examples:
firstNameConstants. Java constants are created by using the keywords
candidateList
result
static
and final
. All letters are capitalized and words are separated by underscores.Examples:
BUTTON_WIDTHJavaBeans Standards. JavaBeans are classes with properties which usually are private instance variables. Private properties can be accessed by other class only through getters and setters. Getters are methods which retrieve a property's value and setters are methods which change a property's value.
STEEL_GAGE
YOUNGS_MODULO
- If the property is not boolean then the method name's prefix should be get (e.g.
getName()
). - If the property is boolean the method name's prefix may either be get or is (e.g.
isValid()
). - The setter method prefix should be set (e.g. setName()).
- To complete the method name just capitalize the first letter of the property's name and append it to the prefix. (e.g. property name:
account
, getter:getAccount()
, setter:setAccount()
). - Setters should be marked public, takes an argument of the property's type and returns void.
- Getters should be marked public, takes no argument and should return a value of the property's type.
public class Person{
private int age;
public int getAge(){
return age;
}
public void setAge(int value){
age = value;
}
}
Events. "The JavaBean spec supports events which allow components to notify each other that something happened (from SCJP 1.6 Study Guide book)". Events are used primarily with GUI applications.
The object which receives the event object is called the listener. Like private instance variables, listeners are added and removed by methods which may follow these conventions:
- Methods which add/register a listener to a component should be prefixed with "add" (e.g.
addMouseListener()
). - Methods which remove/unregister listeners from a component should be prefixed with "remove" (e.g.
removeMouseListener()
). - Register and unregister methods should always end with "Listener".
- addXxxListener and removeXxxListener methods should take an argument with the type of listener.
public void setBalance(double balance)
public double getBalance()
public boolean isEnabled()
public void addWindowListener(WindowListener w)
public void removeWindowListener(WindowListener w)
Examples of INVALID JavaBean method signatures:
Part 3 of the Declarations and Access Controls section will discuss about Class Declaration and Modifiers.
//should be public
private void setBalance(double balance)
//should return a value of the property's type
public void getBalance()
//should start with either set or is
public boolean determineIfSuccessful()
//listener type mismatch
public void addWindowListener(KeyListener k)
//listener type mismatch
public void removeActionListener(WindowListener w)
0 comments:
Post a Comment