Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private Member Access Java

Is the private member access at the class level or at the object level. If it is at the object level, then the following code should not compile

    class PrivateMember {
   private int i;
   public PrivateMember() {
      i = 2;
   }
   public void printI() {
      System.out.println("i is: "+i);
   }
   public void messWithI(PrivateMember t) {
      t.i *= 2;
   }
   public static void main (String args[]) {
      PrivateMember sub = new PrivateMember();
      PrivateMember obj = new PrivateMember();
      obj.printI();
      sub.messWithI(obj);
      obj.printI();
   }
}

Please clarify if accessing the member i of obj within the messWithI() method of sub is valid

like image 243
Prabhu R Avatar asked Mar 24 '09 11:03

Prabhu R


4 Answers

As DevSolar has said, it's at the (top level) class level.

From section 6.6 of the Java Language Specification:

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

Note that there's no indication that it's restricted to members for a particular object.

As of Java 7, the compiler no longer allows access to private members of type variables. So if the method had a signature like public <T extends PrivateMember> void messWithI(T t) then it would be a compiler error to access t.i. That wouldn't change your particular scenario, however.

like image 51
Jon Skeet Avatar answered Oct 20 '22 00:10

Jon Skeet


Note that you don't even need source level access to mess with private fields. By using java.lang.reflect.AccessibleObject.setAccessibe(), all code can access all private members of all other code unless you specify a security policy that disallows it.

private is not by itself a security feature! It is merely a strong hint to other developers that something is an internal implementation detail that other parts on the code should not depend on.

like image 33
Michael Borgwardt Avatar answered Oct 20 '22 01:10

Michael Borgwardt


Neither. Private access is scoped to the enclosing top-level class, so you can access private members of different class in the same top-level class:

class PrivateAccess {
    static class InnerOne {
        private int value;
    }

    static class InnerTwo {
        int getOne ( InnerOne other ) {
            return other.value;
        }
    }
}

The usual meaning of class access means that you have access to privates of other instances of the same type. In Java, private access is determined lexically, not by type.

like image 37
Pete Kirkham Avatar answered Oct 20 '22 00:10

Pete Kirkham


Class level. The idea is that the code of a class (but nothing else) knows how to handle objects of that class.

If you have access to the class source code anyway, there is little sense in "hiding" anything from you.

like image 32
DevSolar Avatar answered Oct 20 '22 02:10

DevSolar