Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • What? People create getters and setters to private variables all the time. It in no way violates the reason for the variable being private. It's called encapsulation. Commented May 16, 2016 at 3:47
  • @4castle - Yes, but from outside the object you can't access the variable itself, all you can do is call the getter and setter. (And in this case the OP said they can't change the constructor function, so they can't add a getter or setter.) Commented May 16, 2016 at 3:48
  • @nnnnnn I'm just objecting to their description of a private variable. In many cases, it isn't done simply to restrict access to the variable, it's done for encapsulation. It's the difference between "reason" and "definition". Commented May 16, 2016 at 3:52
  • @4castle the OP wants to ab able to access the variable without making any changes to the code he provided. So given this code if at all the private variable is made accessible then it violates the purpose of private variable. But yes with code changes it can be done.. Commented May 16, 2016 at 3:52