An instance variablebegins to exists
when it is
created by the
new
operation
Example:
Before the
new BankAccount()
operation, the
instance variablesaccNum,
name and
balance
does not exist:
After the
new BankAccount()
operation, the
instance variablesaccNum,
name and
balance
begins to exist:
When will
an instance variable
cease to exist:
Fact:
An instance variable
will be accessible
as long as
some object reference variable
contains
the address of that object
Example:
We can access the
instance variablesaccNum,
name and
balance
through the
object reference variable stu1
because it contains
the address of that
BankAccount object
Consequence:
An instance variable
will be INaccessible
when
NO object reference variable
contains
the address of that object
Example:
We can
make the
instance variablesaccNum,
name and
balance
become
INaccessible
by updating the
address value stored inside
the
object reference variable stu1:
When will a
instance variablecease to exist:
When instance variables
of an object
becomes
inaccessible
(because no object reference variable in the
Java program contain
its (starting) address), then:
These
instance variables
--- for practical purposes ---
have ceased to exist
The accurate answer to the
question is:
These
instance variables
will cease to exist
after the
garbage collection procedure
has completed execution
(See below for details)
Computer Jargon: garbage and garbage collection
Gargbage:
Garbage =
variables
(that occupy some reserved memory cells)
that are
inaccessible
to the running program
These memory cells
remain reserved until
a complicated process
is used to find them and
mark them as unreserved
(It's a complicated matter to find "garbage")
Garbage collection:
Garbage collection =
the process of
finding all
variables that have become
garbage
and
unreserve their
memory cells.
The program used to
perform the garbage collection process is called
the garbage collector
It can take a
long time for
the garbage collector
to complete.
For practical reasons,
the Java programming system will
only run the
Garbage collector procedure
when the
amount of available
memory
drop below a certain threshold
Accessibility ("scoping") rule for instance variables
The accessibility of
instance variables are
controlled by
access modifiers
(just like class variables)
We will cover this topic more thoroughly
in a later webpage