Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can a "private" method be accessed from a different instance?

Tags:

java

private

Although, this is a very basic code, it seems there is something fundamentally flawed in Java, or the JVM used by the Eclipse IDE I have used to run the code.

The code runs even though it should not (I think)! The code in A.java simply displays "Hello, I am A!"

Here it is:

import java.lang.*;
import java.util.*;
class A {   
      private void methodA() {System.out.println("Hello, I am A!");}   
      public static void main(String[] args) {   
        A a = new A();   
        a.methodA(); }   
} 

I do not understand why, after creating an instance of class A, main() succeeds in running a private method of class A on that instance. Yes, the main method belongs to class A, but it is not accessing the private method from inside the current object in the context of the "this" reference. In fact, since it is a static method, it cannot access non-static members from within the class. Instead of main(), a non-static member method could have invoked methodA() from inside only. But that is another issue since I have not defined any non-static second method.

Now that the inside-view is talked about, let's come back to the point, the outside-view. As you can see, main() attempts to invoke methodA from outside the object and succeeds! Why isn't private getting treated as private?

I am pulling my hair....

Anyone, please reply...

like image 374
softwarelover Avatar asked Feb 20 '14 07:02

softwarelover


People also ask

Can private methods be accessed from another class?

We can call the private method of a class from another class in Java (which are defined using the private access modifier in Java). We can do this by changing the runtime behavior of the class by using some predefined methods of Java. For accessing private method of different class we will use Reflection API.

Where can private methods be accessed?

You can access the private methods of a class using java reflection package. Step1 − Instantiate the Method class of the java. lang. reflect package by passing the method name of the method which is declared private.

How do you access private instance outside the class or subclass?

To access private variables of parent class in subclass you can use protected or add getters and setters to private variables in parent class.. Save this answer. Show activity on this post. You can't access directly any private variables of a class from outside directly.

Is private method accessible from inside all classes in the same package?

The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.


2 Answers

Yes, the main method belongs to class A, but it is not accessing the private method from inside the current object in the context of the "this" reference.

That doesn't matter. That's just not the model of accessibility that Java uses. What's important is the class in which the code is written, not whether it's accessing members in the same object.

This is very useful, as otherwise (for example) it would be impossible to implement an equals method using private members of both classes.

This may not be how you would have chosen to specify the language, but it is how Java is specified. See JLS 6.6.1 for details of accessibility. In particular:

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 protected access is slightly odd here - a protected instance member of class SuperClass can only be accessed within code in SubClass via an expression of either SubClass or a further subclass. But it still doesn't have to be "the same" object.

like image 168
Jon Skeet Avatar answered Nov 10 '22 10:11

Jon Skeet


private modifer

Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

like image 30
Nambi Avatar answered Nov 10 '22 08:11

Nambi