Instances of same class can access private members of each other

From this post onwards, I will spend few days in sharing some little known (if not unknown) features of java language, which we either don’t knew before or didn’t care to think differently. I will really appreciate if all you guys participate in discussion with your thoughts. And for being updated about next discussion point in this series, I will suggest to subscribe through email. You will not be disappointed.

We all know about access modifiers in java. They are four public, protected, default and private. You can read more about them in official java docs.

Java-Access-Specifiers

This official doc and most of reference variables talk about using these access modifiers for controlling the access level “when used in some other class”. Most of us misinterpret this information wrongly, and start thinking in terms of instances of separate class.


Remember, access control is compile time feature and checked when you compile your program. It is applied at class level and not at instance level.

To establish this fact, let’s create two instances of a class and try to access the private members of each other.

package com.howtodoinjava.demo;

public class AccessControlDemo 
{
	//Private member variable
	private String privateMemberVariable = null;
	
	//Private member method
	private String privateMethod(){
		return privateMemberVariable;
	}
	
	public AccessControlDemo(String str) {
		privateMemberVariable = str;
    }
	
	public void demoAccessOtherClass(AccessControlDemo otherInstance)
    {
		//Access private members of second instance
		System.out.println("Private member variable :" + otherInstance.privateMemberVariable);
		System.out.println("Private member method :" + otherInstance.privateMethod());
    }
	
	public static void main(String[] args) {
		AccessControlDemo firstInstance = new AccessControlDemo("first instance");
		AccessControlDemo secondInstance = new AccessControlDemo("second instance");
		
		firstInstance.demoAccessOtherClass(secondInstance);
	}
}

Output:

Private member variable :second instance
Private member method :second instance

As you can see, we are able to access the private members of another instance from same class.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

22 thoughts on “Instances of same class can access private members of each other”

  1. I just was wondering why overriding the “equals” method of a class I could access the private instance variables of the compared Object from “this” class without using getters. Had to google it and arrived to this page that explain it! Thank you!

    Reply
  2. Lokesh , Then we can say that there may be security problem arrives in this scenario and if yes, then how to overcome?

    Reply
  3. Can be done if you write the main method in another class as well, even ur variables are private methods are public and can be done from any where.

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author HARSHIT
    */
    class AccessControlDemo {
    //Private member variable

    private String privateMemberVariable = null;

    //Private member method
    private String privateMethod() {
    return privateMemberVariable;
    }

    public AccessControlDemo(String str) {
    privateMemberVariable = str;
    }

    public void demoAccessOtherClass(AccessControlDemo otherInstance) {
    //Access private members of second instance
    System.out.println(“Private member variable :” + otherInstance.privateMemberVariable);
    System.out.println(“Private member method :” + otherInstance.privateMethod());
    }
    }

    class Access {

    public static void main(String[] args) {
    AccessControlDemo firstInstance = new AccessControlDemo(“first instance”);
    AccessControlDemo secondInstance = new AccessControlDemo(“second instance”);

    firstInstance.demoAccessOtherClass(secondInstance);
    }
    }

    Reply
  4. Hi Lokesh, I don’t see any strange behavior here. You are just invoking the method and accessing the private variable from Object instance.You can do that in any class irrespective of whether that class is of the same type as the Object ( on which you are invoking private methods) or not, nothing fancy here.

    Reply
    • I am pretty sure that even you must have known this minor thing before, but if someone ask you to explain “Access control is compile time feature and checked when you compile your program. It is applied at class level and not at instance level.”, you would have struggled… 🙂

      Reply
  5. i think what JVM did here was, it checked the type of argument of method “demoAccessOtherClass” which is “AccessControlDemo” and since it is the same type as the class in which the method is being written and also the private variable “privateMemberVariable” is written in the same class so it allowed the access of private variable in this method. In a way it’s behaving in the same way as if accessing the member variable in a member function.

    Reply
  6. great sharing…It is applied at class level and not at instance level……If we will pass some different reference(the reference to other class instance ) to the following method instead of “AccessControlDemo”

    public void demoAccessOtherClass(AccessControlDemo otherInstance)

    Then we will not be able to access the “privateMemberVariable” using “otherInstance.privateMemberVariable” inside this method.

    Reply
  7. I like this–Remember, access control is compile time feature and checked when you compile your program. It is applied at class level and not at instance level.

    but from where you got refrenced, because in java API , I have not seen this.
    this could be a good concept.

    Reply
  8. HI,

    Private acsees variable says that-Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

    In your exapmle — it accessing the method and variable in same class.

    You can access the private varibale by–
    1- Variables that are declared private can be accessed outside the class if public getter methods are present in the class

    2- By using reflextion API..

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.