Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do inner classes make private methods accessible?

I don't understand why this compiles. f() and g() are visible from the inner classes, despite being private. Are they treated special specially because they are inner classes?

If A and B are not static classes, it's still the same.

class NotPrivate {     private static class A {         private void f() {             new B().g();         }     }      private static class B {         private void g() {             new A().f();         }     } } 
like image 316
mparaz Avatar asked Mar 19 '09 17:03

mparaz


People also ask

Can inner class access private variables?

It can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public, and default modifier. Like class, an interface can also be nested and can have access specifiers.

Can inner class access private methods of outer class?

Inner classes can even access the private variables/methods of outer classes.

Why Private methods are available in subclasses if they are not accessible in subclasses?

Private methods are inherited in sub class ,which means private methods are available in child class but they are not accessible from child class,because here we have to remember the concept of availability and accessibility.

What is a reason to use an inner class?

Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.


1 Answers

(Edit: expanded on the answer to answer some of the comments)

The compiler takes the inner classes and turns them into top-level classes. Since private methods are only available to the inner class the compiler has to add new "synthetic" methods that have package level access so that the top-level classes have access to it.

Something like this (the $ ones are added by the compiler):

class A  {     private void f()      {         final B b;          b = new B();          // call changed by the compiler         b.$g();     }      // method generated by the compiler - visible by classes in the same package     void $f()     {         f();     } }  class B {     private void g()      {         final A a;          a = new A();          // call changed by the compiler         a.$f();     }      // method generated by the compiler - visible by classes in the same package     void $g()     {         g();     } } 

Non-static classes are the same, but they have the addition of a reference to the outer class so that the methods can be called on it.

The reason Java does it this way is that they did not want to require VM changes to support inner classes, so all of the changes had to be at the compiler level.

The compiler takes the inner class and turns it into a top level class (thus, at the VM level there is no such thing as an inner class). The compiler then also has to generate the new "forwarding" methods. They are made at the package level (not public) to ensure that only classes in the same package can access them. The compiler also updated the method calls to the private methods to the generated "forwarding" methods.

You can avoid having the compiler generate the method my declaring the methods as "package" (the absence of public, private, and protected). The downside to that is that any class in the package can call the methods.

Edit:

Yes, you can call the generated (synthetic) method, but DON'T DO THIS!:

import java.lang.reflect.Constructor; import java.lang.reflect.Method;  public class Main {     public static void main(final String[] argv)         throws Exception     {         final Class<?> clazz;          clazz = Class.forName("NotPrivate$A");                  for(final Method method : clazz.getDeclaredMethods())         {             if(method.isSynthetic())             {                 final Constructor constructor;                 final Object instance;                  constructor = clazz.getDeclaredConstructor(new Class[0]);                 constructor.setAccessible(true);                 instance = constructor.newInstance();                 method.setAccessible(true);                 method.invoke(null, instance);             }         }     } } 
like image 191
TofuBeer Avatar answered Oct 15 '22 11:10

TofuBeer