How to ivoke a method on an object of a private class

When I did it I got a java.lang.IllegalAccessException.
[62 byte] By [hsy541a] at [2007-11-27 7:01:06]
# 1
You need to provide more detail.
tschodta at 2007-7-12 18:51:55 > top of Java-index,Core,Core APIs...
# 2

There's quite enough detail here.

You're supposed to get an IllegalAccessException, that's what 'private' does. If you need to do this you need the appropriate RuntimePermission and you need to call Method.setAccessible(): see [url]http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/AccessibleObject.html#setAccessible(java.lang.reflect.AccessibleObject[],%20boolean)[/url].

ejpa at 2007-7-12 18:51:55 > top of Java-index,Core,Core APIs...
# 3

> There's quite enough detail here.

Well, I read the subject literally and triedclass Test {

public static final void main(String[] arg) {

Test test = new Test();

test.run();

}

Test() {}

void run() {

Foo foo = new Foo();

System.out.println(String.valueOf(foo));

}

private class Foo {

private Foo() {}

}

}

and it did not give any exceptions.

tschodta at 2007-7-12 18:51:55 > top of Java-index,Core,Core APIs...
# 4
Well 'private' does have a well-known meaning in Java, and it's been working in Java for at least 11 years. Surely you knew that?Re your example, special rules apply to inner classes. The access modifiers aren't tested by the enclosing class.
ejpa at 2007-7-12 18:51:55 > top of Java-index,Core,Core APIs...
# 5

> Well 'private' does have a well-known meaning in

> Java, and it's been working in Java for at least 11

> years. Surely you knew that?

The combination "private class" seemed unusual

and made me de-focus from the rest of it.

> Re your example, special rules apply to inner

> classes. The access modifiers aren't tested by the

> enclosing class.

Yes. The only way I am aware of of having a "private class" (as the subject implies) is to have a nested class.

I agree one would be able to access methods on an instance of such a class only from the embedding class (or peer nested classes).

tschodta at 2007-7-12 18:51:55 > top of Java-index,Core,Core APIs...