0% found this document useful (0 votes)
28 views

Summary 3 - Variables: Fields vs. Parameters vs. Local Variables

Fields, parameters, and local variables differ in scope and lifetime: 1) Fields are associated with objects and can be referred to anywhere within the class, lasting as long as the object exists. 2) Parameters are passed into methods and exist from when the method is called until it returns. 3) Local variables are declared within methods and exist only between their declaration and the end of the enclosing block.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Summary 3 - Variables: Fields vs. Parameters vs. Local Variables

Fields, parameters, and local variables differ in scope and lifetime: 1) Fields are associated with objects and can be referred to anywhere within the class, lasting as long as the object exists. 2) Parameters are passed into methods and exist from when the method is called until it returns. 3) Local variables are declared within methods and exist only between their declaration and the end of the enclosing block.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Summary 3 – Variables: Fields vs. Parameters vs.

Local variables
What is this?
Type of Scope – where it is legal to refer Lifetime – when it is legal to refer to
Description
variable to the variable the variable
Associated with objects of the class in The lifetime of the object with which
Field The class in which it is defined
which the field is defined they are associated
Associated with a method; its value is given The body of the method for which it From when its method is called until the
Parameter
to its method when the method is called is a parameter time when its method is exited
Local From the point at which it is declared From the time it is declared to the time
Defined within a method
variable to the end of its enclosing block that its enclosing block is exited

Example (from JavaEyes)


public class Eye extends JPanel implements MouseMotionListener {
private static final int DEFAULT_RADIUS = 25;
protected EyeBall eyeBall; DEFAULT_RADIUS, eyeball, eyeColor and eyeRadius are fields.
protected Color eyeColor; Every Eye has these four items of data associated with it (the
protected int eyeRadius; DEFAULT_RADIUS is shared by all Eyes – see the Summary on static).
...
event is a parameter of the method mouseMoved.
public void mouseMoved(MouseEvent event) {
Point mousePoint = new Point(event.getX(), event.getY());
this.look(mousePoint);
} mousePoint is a local variable of the method mouseMoved.
}

For further study:


o Big Java, section 3.4 Instance Fields, describes fields.
o Big Java, section 2.4 Method Parameters and Return Values, describes how parameters are used, and section 3.8 Implicit
and Explicit Parameters distinguishes explicit parameters of a method from its implicit parameter.
o Big Java, section 3.7 Categories of Variables, discusses lifetime, and section 8.8 Scope discusses scope.
o Authors of this summary: David Mutchler.
o See also the Summaries on Variables: Primitive Type versus Object Type

You might also like