6

I've been thinking about classes, and specifically about anonymous inner classes. This led me to wonder what is the access type of an anonymous inner class?

I realize in most cases this won't change anything but it might make a difference for reflection for example. I've seen several questions asking about having problems using reflection to access the contents of anonymous inner classes.

I did find this question (one example of this problem): access exception when invoking method of an anonymous class using java reflection

And this answer which suggests it is private but the writer was unable to confirm: https://stackoverflow.com/a/2659647/3049628

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • I think the question that you quote doesn't shed much light on the class itself. The error is because of a bug, and the bug is about the method's accessibility, not the class. The class itself is not a member, and therefore "access" to it is meaningless. – RealSkeptic Nov 23 '15 at 13:55

2 Answers2

4

Looks like they are public, but the JDK's Method.invoke implementation has a (long-running) bug. See bug 4071957.

The linked bug does not really correspond to our situation, however it aggregates all types of Method.invoke access control problems over inner classes and was marked as duplicate of bug 4819108 which was linked in the accepted answer of the SO question you mentionned.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • 1
    Whoa, `Submit Date: 1997-08-14` (!!) – TayTay Nov 23 '15 at 13:27
  • Yeah, looks like they have wanted to change the classfile format since java 1.1, which would help fixing this problem. It has been updated in 2014 so maybe they haven't totally abandoned all hope to do it... – Aaron Nov 23 '15 at 13:40
3

The class itself seems to have package of outer class.

getModifiers() value is 8, which as per comments is: static, package private.

Its static (why ?). I can't refer to Main.this from inside of an anonymous class in Main.

Its package private. Still, access level of an anonymous class doesn't even mean much, since you can't use its type at compile time like a normal class.

https://ideone.com/vzbdQ5

// JDK 8
import java.lang.reflect.Modifier;

public class Main {
    public static void main(String[] args) throws Exception {
        Class anon = new Main() {}.getClass();

        System.out.println("Name:" + anon.getName()); // Main$1
        System.out.println("Anonymous:" + anon.isAnonymousClass()); // true
        System.out.println("Package:" + anon.getPackage()); // null

        System.out.println("public ? " + Modifier.isPublic(anon.getModifiers())); // false
        System.out.println("private ? " + Modifier.isPrivate(anon.getModifiers())); // false
        System.out.println("protected ? " + Modifier.isProtected(anon.getModifiers())); // false

        assert anon.newInstance() instanceof Main;
        assert Class.forName(anon.getName()).newInstance() instanceof Main;
    }
}
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • @assylias Thanks. tested again, anon class has same package as outer class. – S.D. Nov 23 '15 at 14:30
  • I think it's only static because it's declared in a static method. In any other method, it wouldn't be static. As for it being "package private" - it's actually just the default, which is interpreted as "package private". But this is meaningless for a class *which is neither a member nor top-level*. – RealSkeptic Nov 23 '15 at 14:59