|
Could print() use the reference to the original String object to change the contents of that object? A good answer might be:No---because String objects are immutable. Not even the main() method can change the original object. | |
Immutable Strings
It is good that String objects are immutable (they can't be changed) because the main() method can be sure that the message is completely under its control. Although the print() method gets a copy of the reference, it can't change the original object. Not all objects are immutable. Important: Public instance variables of objects can be changed by any method that has a reference to the object, even if the reference is in a formal parmater. (If an instance variable is neither public nor private it can be changed by a method that is in the same package. For now, all of our code is in the same package, so the effect is the same as if it were public.) Here is a program that has a user-defined class and a testing program: The main() method uses the default constructor of class MyPoint. This is the constructor you get automatically if you do not define one yourself. | |
QUESTION 10:Click Here after you have answered the question |