1

Just for the sake of example I am putting in String as member variable. But there is complex structure originally.

public class ClassA {

    private final String test ;

    public ClassA(String str) {
        test = str;
    }

    @Override public int hashCode() {
        return test.hashCode();
    }

    @Override public boolean equals(Object obj) {
        return obj instanceof ClassA && test.equals(((ClassA)obj).test);
    }

}


public class ClassB {

    public static void main(String args[])
    {
        ClassA obj1 = new ClassA("abc");
        ClassA obj2 = new ClassA("def");
        obj1.equals(obj2);
        obj2.test;//not valid
    }
}

From what I know access to the private variable test of obj1 is there only within the methods of ClassA and these methods should be called from the context of obj1.

When I called obj1.equals(obj2) test variable of obj2 is accessible from the obj1's context.

So can we access the private variables of any object of type ClassA from within ClassA methods.

vjk
  • 2,163
  • 6
  • 28
  • 42

5 Answers5

0

As you write yourself:

So can we access the private variables of any object of type ClassA from within ClassA methods.

This is correct.

rusmus
  • 1,665
  • 11
  • 18
0

Private instance variables are private to the class in which they are defined.

It does not mean that they are private to the instance of the variable values are stored in.

TheBlastOne
  • 4,291
  • 3
  • 38
  • 72
0

The following

From what I know access to the private variable test of obj1 is there only within the methods of ClassA and these methods should be called from the context of obj1.

is only partially correct: the method may be also a static method in ClassA (not in context of ANY instance of ClassA), meaning you call ClassA.staticMethod(obj1); and there you would have access to obj1.test.

The following has the affirmative answer:

So can we access the private variables of any object of type ClassA from within ClassA methods?
V G
  • 18,822
  • 6
  • 51
  • 89
0

"private" doesn't mean private to the object but private to the class. One of the main reasons for using private fields is implementation hiding, but there would be no use in hiding the classes implementation from the class itself.

Just keep in mind that "ClassA" in this case really means "ClassA only". You won't be able to access private members from a subclass of ClassA.

piet.t
  • 11,718
  • 21
  • 43
  • 52
0

Ofcourse yes. Private access specifier cant stop the class to access its own variables. Its irrespective of any access specifiers.Otherwise how your getter and setter method are going to work.

This is fundamental of encapsulation. You can access one class's variables by creating its object. but same class by default would have access to its members.

Again as someone rightly pointed out even private members of super class are not visible or accessible to its subclass. but that is a different concept.

You can go through below nice articles/blogs

http://inheritingjava.blogspot.in/2011/01/chapter-4-access-modifiers.html

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Suvasis
  • 1,451
  • 4
  • 24
  • 42