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

JAD Advantages and Disadvantages

What is JAD and how are users involved? What are advantages and disadvantages of JAD?