header

Data Declarations

Instance Variables

An instance variable is a data element whose value can be altered and which has (potentially) a different value for each instance. The values of the instance variables represent the state of the object. Instance variables can be public but seldom are.

Declaration: private type varId;

Class Constants

A class constant is a data element whose value cannot be altered. Use a private class constant if a fixed value is needed in two or more class members but the value does not need to be publicly accessible.

Declaration: private static final type constantId = constantValue;

Use a public class constant if a fixed value needs to be publicly accessible. You should write a documentation comment for each public class constant.

Declaration: public static final type constantId = constantValue;

Class Variables

A class variable is a data element whose value can be altered and which has the same value for every instance. Class variables are seldom needed. Class variables can be public but seldom are.

Declaration: private static type varId;

Local Constants

Local constants represent fixed values that are needed within a single method. Use a local constant if the value is needed two or more times or if naming the value would make your code more readable.

Declaration: final type constantId = constantValue;

Local Variables

Local variables represent a value that is subject to change and is needed within a single method.

Declaration: type varId;
Declaration: type varId = initialValue;

Identifiers

Variable identifiers should be lowercase with each word after the first capitalized. Here are some examples:

width, hourlyWage, totalCostPerBox

Constant identifiers should be uppercase with words separated by an underscore character. Here are some examples:

PI, UNIT_COST, KILOGRAMS_PER_POUND

Identifiers can also contain digits (0..9) but don't use digits unless you have a compelling reason to do so.

See also: Data Types, Introduction to Objects

Java Tutorial: https://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html