CSE 115 Tutorial 3 Variables

Designer: Yingrui Liu

  Scope and lifetime
Home
Declaration
Assignment
Scope & lifetime
Relationship & Methods

Variables have lots of properties. Now we know that a variable has a type, a name and a vaoue. The type and name of the variable are required components of a variable declaration. The value of a variable is set using an assignment statement.

Every variable also has a scope and a lifetime.
Definitions:
Scope of a variable: the region of the program text in which the variable is accessible.
Lifetime of a variable: the period of time during the execution of a program during which it exists.

  Scope of the variable Life time of the variable
Local Variable From its point of declaration to the end of the body in which the declaration of the variable occurs.
A Local Variable cannot be used before its declaration.
Begins when the method that the variable is declared in is called and ceases when the execution of the method is finished
Instance Variable

The entire body of the class definition in which the instance variable is declared, regardless of where in the class definition the declaration occurs.In other words, a Instance Variable can be used before its declaration as long as it is used in the class that it is declared in.

Begins when the object of which it is a part of is created and exests as long as its object exists.


back to top