Categories
Computing Java

Java private is not so private

The Java private access modifier does not mean instance visibility, it means *class* visibility

Share

I am sure that many of us feel very warm and safe in knowing fundamental facts about Java. For example, a class variable like

private int h;

cannot be modified by any object other than the class it exists in, right?
Wrong – otherwise why would I waste my time here? The issue popped up when I was writing my solution to a class assignment and I noticed that instances of the same class can access each other’s private member variables.
Further investigation on the Java Tutorial reminded me of the very fundamental fact that the private access modifier specifies class visiblity, NOT instance visiblity. Definitely not something anyone would recommend using, just something you should keep in mind. Always.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Share