Explain why aliases can cause trouble for programmers who are not aware of them.

Explain why aliases can cause trouble for programmers who are not aware of them.

Answer:  Aliases arise in Java when variables refer to the same object.  If one of those variables is used to pass a message to the object in such a way that the object is changed, then the other variables now refer to a changed object.  Unless the programmer is careful, he/she may not realize that a change has been made to the object.  Consider a class Foo that has an instance data x and a method incr that adds one to x.  Assume a and b are both of type Foo, where a’s x is 10.  Consider the following two instructions:  b = a;  b.incr( );  The result is that a/b’s value x is now 11, but if the programmer is not aware of the aliasing, then he/she might expect a’s x to still be 10.

Comments

Popular posts from this blog

Discuss the seven characteristics of useful information.

Why have accounting software packages been designed with separate transaction modules?

A laboratory assistant prepared solution of 0.8 M, 0.6 M, 0.4 M, and 0.2 M sucrose, but forgot to label them. After realizing the error, the assistant randomly labeled the flasks containing these four unknown solutions as flask A, flask B, flask C, and flask D.