- The scope of a variable:
All variables are members of the block of code in which they are
defined. This allows for two scope designations:
- Local variables are defined inside the block of code
specifying a method body, and, therefore belong to that method. They
can only be used within the body of the method where they are
declared.
- Instance variables are defined outside of the body of any
method, but inside the body of a class. As such, instance variables
belong to a class, i.e., they are members of a class. They can be
used anywhere inside of the class definition. Because they are
members of a class, they also exist in every object of that class
(i.e., if the class name is Dog, then every Dog object will
contain any instance variables defined in the Dog class). All
public members of a class (both instance variables and methods)
can be used by methods that are not members of the class.
- Encapsulation
Instance variables are typically encapsulated by declaring them
to have private access. A class member that is declared to be
private can only be accessed by other class members. Public methods
provide the interface (i.e., user access) to private instance
variables.
- Parameters and Return Values
A return value is used to send information (a single value) from the
callee method to the caller method. Parameters facilitate the sending
of information (one value per parameter) from the caller method to the
callee method. A parameter is just like a local variable (belonging
to the callee method) except that it gets its initial value from the
caller method. The actual parameter establishes the initial
value of the formal parameter.