Rahul Sudip Bose wrote:I declare a member variable private in a class. Then, i let one of the member methods return a reference to that private variable. This seems to be dangerous to me. I compile one such program and ran it successfully. Why doesnt the compiler prevent/warn me from returning a reference to a private variable ?
here is my program which works :
This is because you may need to modify a private member of a class so
java allows you to do that through getters and setters.
Your method acts likely as getters. So this is as expected. Reffering to the term "
immutable" the setters methods sets the values of the variables, if immutable then a new object is assigned to the reference.