Variables

Chapter 6. Summary

characteristic

Local variable

Instance variable

Class variable

Declaration in

Method / Constructor / Block

Inside the class, but outside a method.

Inside the class, but outside a method and declared as static

Usage

Store values used in computations

Store values that must be referenced by more than one method

These are used as constants(Value never change)

Lifetime

Created when method or constructor is entered.

Destroyed on quit.

Created when instance of class is created with new. 
Destroyed when there are no more references to enclosing object

Created when the program starts. 
Destroyed when the program stops.

Scope/Visibility

Local variables (including formal parameters) are visible only in the method, constructor, or block in which they are declared. Access modifiers (private, public, ...) can not be used with local variables. All local variables are effectively private to the block in which they are declared. No part of the program outside of the method / block can see them. A special case is that local variables declared in the initializer part of a forstatement have a scope of the for statement.

Instance (field) variables can been seen by all methods in the class. Which other classes can see them is determined by their declared access:

private should be your default choice in declaring them. No other class can see private instance variables. This is regarded as the best choice. Define getter and setter methods if the value has to be gotten or set from outside so that data consistency can be enforced, and to preserve internal representation flexibility.

Default (also called packagevisibility) allows a variable to be seen by any class in the same package. private is preferable.

public. Can be seen from any class. Generally a bad idea.

protected variables are only visible from any descendant classes. Uncommon, and probably a bad choice.

Same as instance variable, but are often declared public to make constants available to users of the class.

Declaration

Declare before use, anywhere in a method or block.

Declare anywhere at class level

Declare anywhere at class level with static.

Initial value

None. Must be assigned a value before the first use.

Zero for numbers, false for booleans, or null for object references. May be assigned value at declaration or in constructor.

Same as instance variable, and it addition can be assigned value in specialstatic initializer block.

Access from outside

Impossible. Local variable names are known only within the method.

Instance variables should be declared private to promote information hiding, so should not be accessed from outside a class. However, in the few cases where there are accessed from outside the class, they must be qualified by an object (eg, myPoint.x).

Class variables are qualified with the class name. They can also be qualified with an object, but this is a deceptive style.

Name syntax

Standard rules.

Standard rules, but are often prefixed to clarify difference from local variables, eg withmy, m, or m_ (for member) as in myLength, or this as inthis.length.

static public finalvariables (constants) are all uppercase, otherwise normal naming conventions. Alternatively prefix the variable with "c_" (for class) or something similar.