您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 19

 Variables can change

By their nature, a variable can change its value. A variable declared for referring to an object of a certain class can refer to any objects of that class. So such a variable might at one point be referring to one object, and at another point be referring to a different object.

For example:

// object variable declaration

BankAccount accountToBalance;

...

accountToBalance = account1;

display( accountToBalance.getBalance() );

accountToBalance = account2;

display( accountToBalance.getBalance() );

So at one point during the execution of these statements, variable accountToBalance was referring to the same object as account1, and in a later statement accountToBalance was referring to a different object, the one referred to by account2.

This illustrates:

  • a variable can refer to different objects at different times
  • two (or more) variables can refer to the same object

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg