Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Inner Classes Change Access Rights Of Parent Members?!?

Reply
Thread Tools

Inner Classes Change Access Rights Of Parent Members?!?

 
 
cppaddict
Guest
Posts: n/a
 
      08-23-2004
I read that if you access a parent class's private memebers or methods
from within an inner class, those members of methods will
automatically and silently be converted to having package access. This
seems dangerous and I'd like to know how I could design around it.

Here is my current dilemma. I have an EventHandler class whose
handleEvent() method changes with the object's state. I've implemented
this using the Strategy Pattern, where the Strategy objects are inner
classes of EventHandler. The problem is that these Strategy objects
need access to certain private members and methods of their parent.
There is no reason, however, to give package access to these members
and methods. What can I do? Or does this suggest that I need a design
change? Other than this issue, though, I'm quite happy with the
design.

Thanks for any thoughts,
cpp
 
Reply With Quote
 
 
 
 
cppaddict
Guest
Posts: n/a
 
      08-23-2004
I found a good answer for this somewhere else:

When inner classes access private fields or methods, the compiler
generates new package-private methods
with names like "access$000":

<code>
import java.lang.reflect.*;
public class Test {
private void x() {
}
class Y {
public void y() {
x();
}
}
public static void main(String[] args) {
Method[] methods = Test.class.getDeclaredMethods();
for(int i=0; i<methods.length; ++i)
System.out.println(methods[i].getName());
}
}
</code>

So it's not correct that the access to fields or methods is changed,
just that additional methods are added. Unless you're in the habit of
writing method names that contain '$', I think it's unlikely that
you'll directly call these new methods, and if you do, it should be
easy to spot!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Debate: Inner classes or public classes with package access? Christian Bongiorno Java 5 08-30-2004 08:14 AM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments