Content deleted Content added
Cleaning up some weird code syntax highlighting by using a script block instead of code-per-line |
Source, not script. Boy howdy did I mess that preview up. |
||
Line 97:
Although it also appears to break the intent of final variables, modifying the value from another class (by getting the reference via a public getter method and changing its value) ''does'' change the value of the referenced object. For instance, according to the semantics of the final keyword, the following unit test should output "68", while in fact it outputs "82" and throws an assertion exception:
<
package test;
import java.sql.Date;
Line 134:
}
}
</
The reason for this is that declaring a variable final only affects the reference, not the value of the variable, and Java always passes by reference, which creates a different reference to the same value in the second class. That the value of the private field can be changed without a public setter implies that encapsulation is weak at best, and shouldn't be counted on to protect key values, even in combination with the final keyword.
|