Thursday, November 5, 2009

SCJP 1.6 Study Guide: Declarations and Access Controls Part 2

This is Part 2 of Declarations and Access Controls Section. Part 1 was just a refresher if you have been away from Java for long. I will discuss legal identifiers, keywords, Sun/Java naming conventions, and the JavaBean naming convention.

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:
  1. Must start ONLY with a letter, a dollar sign ($), or an underscore (_).
  2. After the first character, the identifier can contain any combination of letters, numbers, dollar signs and underscores.
  3. Must not be one of the Java keywords.
  4. Identifiers are case-sensitive. So bar is different from Bar (Not exactly a rule but you need to remember this when you are using identifiers)
Examples of LEGAL identifiers:
int buzz;
int $name;
int _AgE;
int $$_$;
int an$unnecessarily_long$identifier_;
Examples of ILLEGAL identifiers:
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:
Person
DepartmentHead
MyPetStore
Interface names should be adjectives.
Examples:
Serializable
Comparable
Runnable
Methods. 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.

Examples:
drawShape
createPlayer
interpolateValue
Variables. Should follow camelCase convention as well. Variable names are usually nouns. Make them short but descriptive.

Examples:
firstName
candidateList
result
Constants. Java constants are created by using the keywords static and final. All letters are capitalized and words are separated by underscores.

Examples:
BUTTON_WIDTH
STEEL_GAGE
YOUNGS_MODULO
JavaBeans 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.
  • 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.
Example
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.
Examples of VALID JavaBean method signatures:
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:

//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)
Part 3 of the Declarations and Access Controls section will discuss about Class Declaration and Modifiers.

0 comments:

Post a Comment