Inner Classes Changing Access Rights Of Parent  Members

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,
John

When inner classes access private fields or methods, the compiler generates new package-private methods
with names like "access$000":
import java.lang.reflect.*;
public class X {
    private void x() {}
    class Y {
        public void y() {
            x();
    public static void main(String[] args) {
        Method[] methods = X.class.getDeclaredMethods();
        for(int i=0; i<methods.length; ++i)
            System.out.println(methods.getName());
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!

Similar Messages

  • Custom object classes and access rights

    Hi,
    I have added a few object classes to the NDS schema; objects
    belonging to one of them should be able to authenticate against the
    directory and retrieve some attributes. I managed the login part having
    the class inherit from ndsLoginAttributes, but if I login as the object
    itself, I can't retrieve any attributes. I can browse the entry (it's a
    container), but all I get are DNs and objectclass attributes. Is there a
    way to grant the object the right to retrieve its own attributes, or
    some of them, through the Java LDAP interface?
    Thanks,
    Juan
    jheguia
    jheguia's Profile: http://forums.novell.com/member.php?userid=84575
    View this thread: http://forums.novell.com/showthread.php?t=415769

    Hello,
    I found a solution which is *almost* the right one. Basically I
    deleted the class and created it again with a default ACL:
    X-NDS_ACL_TEMPLATES ( '2# subtree#[Self]#[All Attributes Rights]' )
    This allows the object to do as it pleases with its own attributes. I'd
    prefer it to be only able to read them, but I haven't found a syntax for
    ACLs. Is there anything I can read to see how to fine tune the access
    rights templates?
    Thanks,
    Juan
    jheguia
    jheguia's Profile: http://forums.novell.com/member.php?userid=84575
    View this thread: http://forums.novell.com/showthread.php?t=415769

  • Why Inner class cannot access static variables

    Why is it that inner class can use only static final variables of the outerclass, and not ordinary static variables of the outer class. "Yes the JLS sepcifies that only final static variables can be used inside an inner class, esp a non blank final variable". But why this restriction.
    Thanks.

    so what are final static variables treated as if they
    are not variables. So if the final static value is
    not loaded when the class is loaded how will the
    class know about the value.??The actual value wil be substituted for the name of a static final value at compile time. That's why you can use them in switch statements where you can't use any variable variable.
    This is something to watch out for, by the way, because if you use a public static final value from one class in another the actual value will be compiled into the using class, so if you change the value where it's defined the class using it will have the old value until it's recompiled.

  • MacBook Pro won't start after changing access rights

    I did something very wrong. Unfortunately I changed the access rights of Macintosh HD for "everyone" to none.
    Result, my MacBook Pro won't start anymore.
    Already tried the installation disk, reset password for root and users. No result.
    Help program "check acces rigths". Didn't work either.
    How do I reset the access rights of Macintosh HD?

    https://discussions.apple.com/docs/DOC-2240
    The first solution is what you're looking for.

  • Inner class variable access

    Hi all
    If for example I pass a JButton to a method or construcrtor of another Class. Is there a way to make this JButton accessable from all other methods or subclasses in that Class?
    It's something that has caused me problems in the past, I've been able to work around the problem but I think it would make life much easier if this were possible.
    Thanks

    Thanks for the quick response.
    So you mean I can do something like:
    public class MyClass
    private JButton MemberButton;
    public MyClass( JButton aButton )
                   MemberButton = aButton;
    }

  • Your Opinions: Inner Classes Need static Members

    Hi All,
    I want to solicit opinions for a minor change to the way inner classes work. I submitted this as an RFE to Sun and they rejected it, really without giving a reason. I'd like to know your opinions. If there is strong support I will repost the RFE.
    As you probably know, inner classes cannot have static members. The following generates a compiler error:import java.util.*;
    public class MyClass {
       class MyInnerClass {
          // Next line causes compiler error...
          static Map m = new HashMap();
    }In order to get around this you have to make the Map variable a static member of the containing class:import java.util.*;
    public class MyClass {
       static Map m = new HashMap(); // so much for encapsulation...
       class MyInnerClass {
    }I am suggesting that inner class be allowed to contain static members. Here's my reasoning...please comment:
    There are times when members (i.e., fields and methods) rightfully belong to the class as a whole, not to any particular instance of a class. I'm sure we've all found times when it was necessary to have static members in our classes. The same issues that necessitated using static members in top-level classes make them desirable for inner classes as well.
    Designing a class as an inner class is a step toward encapsulation. By forcing static members that logically belong in an inner class to be declared in the containing class is to crack the encapsulation, IMHO.
    Even though a containing class has access to all of an inner class' members (including private members) and vice versa, I think the notion of inner static members still is more OO-ish.
    What are your opinions? Would allowing inner classes to contain static members make Java more object oriented? I think it would.
    Technically, I don't think there's any reason this cannot work since the JVM has no notion of inner classes, per se.
    What do you think?

    an inner class is effectively a non static instance
    variable of its enclosing class. Instance member, but not a variable. it's a class, a type, not a variable.
    >
    I think the problem here is that making a field static
    means more than just that that field and its value are
    common to every instance of the class. It means that
    the value is valid without an instantiation of that
    class.
    Since the class itself must be instantiated (it is
    not static), What do you mean, excatly, by "_must_ be instantiated"? You are not ever "required" to instantiate anything unless you want to use it.
    you can't have static member data inside it. I don't see how this follows from the previous part of the statement.
    How would you reference the static member data of
    the inner class? You would have to specify an
    instance of the inner class, and since this breaks
    the meaning of static, you can't have static members
    in an inner class.How about outerObj.InnerClass.staticMember The syntax is well defined. The question at hand is, do we really want to allow this? The syntax to do this should only be an issue after that question has been answered in the affirmative. The people at Sun have decided not to allow it, so for now, syntax is a non-issue.
    >
    if you wanted a static member in an inner class you
    could put it in a super class of the inner class...Or in the enclosing class, as suggested in the orginal post.

  • How to access var in outter class inside inner class

    I've problem with this, how to access var number1 and number2 at outter class inside inner class? what statement do i have to use to access it ? i tried with " int number1 = Kalkulator1.this.number1; " but there no value at class option var y when the program was running...
    import java.io.*;
    public class Kalkulator1{
    int number1,number2,x;
    /* The short way to create instance object for input console*/
    private static BufferedReader stdin =
    new BufferedReader( new InputStreamReader( System.in ) );
    public static void main(String[] args)throws IOException {
    System.out.println("---------------------------------------");
    System.out.println("Kalkulator Sayur by Cumi ");
    System.out.println("---------------------------------------");
    System.out.println("Tentukan jenis operasi bilangan [0-4] ");
    System.out.println(" 1. Penjumlahan ");
    System.out.println(" 2. Pengurangan ");
    System.out.println(" 3. Perkalian ");
    System.out.println(" 4. Pembagian ");
    System.out.println("---------------------------------------");
    System.out.print(" Masukan jenis operasi : ");
    String ops = stdin.readLine();
         int numberops = Integer.parseInt( ops );
    System.out.print("Masukan Bilangan ke-1 : ");
    String input1 = stdin.readLine();
    int number1 = Integer.parseInt( input1 );
    System.out.print("Masukan Bilangan ke-2 : ");
    String input2 = stdin.readLine();
    int number2 = Integer.parseInt( input2 );     
         Kalkulator1 op = new Kalkulator1();
    Kalkulator1.option b = op.new option();
         b.pilihan(numberops);
    System.out.println("Bilangan yang dimasukkan adalah = " + number1 +" dan "+ number2 );
    class option{
    int x,y;
         int number1 = Kalkulator1.this.number1;
         int number2 = Kalkulator1.this.number2;
    void pilihan(int x) {
    if (x == 1)
    {System.out.println("Operasi yang digunakan adalah Penjumlahan");
            int y = (number1+number2);
            System.out.println("Hasil dari operasi adalah = " + y);}
    else
    {if (x == 2) {System.out.println("Operasi yang digunakan adalah Pengurangan");
             int y = (number1-number2);
             System.out.println("Hasil dari operasi adalah = " + y);}
    else
    {if (x == 3) {System.out.println("Operasi yang digunakan adalah Perkalian");
             int y = (number1*number2);
             System.out.println("Hasil dari operasi adalah = " + y);}
    else
    {if (x == 4) {System.out.println("Operasi yang digunakan adalah Pembagian ");
             int y = (number1/number2);
             System.out.println("Hasil dari operasi adalah =" + y);}
    else {System.out.println( "Operasi yang digunakan adalah Pembagian ");
    }

    Delete the variables number1 and number2 from your inner class. Your inner class can access the variables in the outer class directly. Unless you need the inner and outer class variables to hold different values then you can give them different names.
    In future place code tags around your code to make it retain formatting. Highlight code and click code button.

  • Private inner classes

    I'm trying to complete a "turn the lightbulb on and off" program, but when I try to draw circle2 in the
    ButtonListener class I get an error message cannot find symbol. This is in reference to the Graphics
    variable "page" created in the paintComponent method below. Shouldn't the inner class, private or
    public inherit all data variables including objects from the parent class, in this case, the Bulb class? The code is below.
    By the way, this IS NOT a school assignment so any help would be appreciated. I'm just trying to learn
    this language.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Bulb extends JPanel
         private JButton push;
         private Circle circle, circle2;
         private final int DIAMETER = 100;
         private final int X = 10;
         private final int Y = 10;
         public Bulb()
              circle = new Circle(DIAMETER, Color.white, X,Y);
              circle2 = new Circle(DIAMETER, Color.yellow, X, Y); // to separate class
              push = new JButton("Turn on the Bulb");
              push.addActionListener(new ButtonListener());
              add(push);
              setPreferredSize(new Dimension(500, 500));
              setBackground(Color.black);
         public void paintComponent(Graphics page)
              super.paintComponent(page);
              circle.draw(page);
    private class ButtonListener implements ActionListener
              public void actionPerformed(ActionEvent event) //PROBLEM AREA. I GET ERROR MESSAGE STATING
    // "CANNOT FIND SYMBOL" IN REFERENCE TO VARIABLE "PAGE."
    // I THOUGHT THE INNER CLASS INHERITS ALL DATA FROM
    // PARENT CLASS SUCH AS "PAGE."
                   circle2.draw(page);
    }

    There are fields, which are associated with either a class or an object (and thus live in the heap in an object on the heap), and there are local variables, which are associated with methods and threads (i.e., a method invoked within a thread, and which thus live on the stack).
    They're not the same thing.
    You can't use a local variable in your paintComponent method in a different method.
    Anyway you're designing your class wrong. Think model-view-controller. You have the model: a bunch of state and possibly behavior that represents the thing being seen, modified, and displayed. You have the view, which is how you see the model. And you have the controller, which modifies the model.
    Your event handlers are part of the controller. They should change the model.
    Your paintComponent method is part of the view.
    So the event handlers should change some data, e.g., add a note that a circle should be displayed.
    Then your paintComponent method should look at the data and act accordingly -- e.g., see that there's a circle to be displayed, and display it.

  • Why method local inner class can use final variable rather than....

    Hi all
    Just a quick question.
    Why method-local inner class can access final variable defined in method only?
    I know the reason why it can not access instance variable in method.
    Just can not figure out why??
    any reply would be appreciated.
    Steven

    Local classes can most definitely reference instance variables. The reason they cannot reference non final local variables is because the local class instance can remain in memory after the method returns. When the method returns the local variables go out of scope, so a copy of them is needed. If the variables weren't final then the copy of the variable in the method could change, while the copy in the local class didn't, so they'd be out of synch.

  • Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var

    i fail to make an inner class distinguish among function parameter and outer class variable.
    consider the following code:
    public class a {
         int var; // i want to access this one using inner class.
         public a(int var) {
              this.var = var;
              class b {
                   public void fun() {
                        var = 100;
    i get an error a.java:9: local variable var is accessed from within inner class; needs to be declared final.
    when i change the code to this:
    public class a {
         int var; // i want to access this one using inner class.
         public a() {          
              class b {
                   public void fun() {
                        var = 100;
    it compiled no problem.it seems that inner class is accessing function parameter rather than outer class member.
    is there any syntax i can use to make me access the outer class member variable whithout renaming or removing the function parameter?
    thank you.

    a.this.var = 100; //Use this in the inner class.Amazing syntax -:)

  • Anon inner classes + final vars

    Ive always taken it for granted that anon inner classes can access outside variables only if they are static.
    Googling this yields about the same information but no good explaination.
    can someone kindly explain why the JVM wants this restriction? i suppose it must have something to do with the way objects are handled internally.
    ill give dukes!

    The "final" restriction only applies to variables within a method which you want to use outside the method. I would assume it is down to
    1) The variable goes out of scope when the method ends.
    2) init().field and init.AnonActionListener().field would actually be different variables, changing init().field would not be replicated to init.AnonActionListener().field. This would be very confusing.
    �8.1.3 does not give any reason for this.

  • Why inner classes cannot have static declarations ?

    Hi Friends,
    When i tried to make static declarations on a inner class which is non static, i am getting compilation error saying "inner classes cannot have static declarations". I want to know reason behind this implementation.
    Code which i have tried:
    public class TestOuter
    class TestInner{
    static int i =10;
    public static boolean validate(int a){
    if(a==0)
    System.out.println("Invalid data");
    return false;
    return true;
    public static void main(String a[]){
    boolean result = new TestOuter.TestInner.validate(0);
    System.out.println("Result="+result);
    Thanks,
    Shiju V.

    so I think if the
    outer class is not static , then Inner class can't be
    static as well. This is incorrect. An enclosed class can be indeed static while the outer is not, and vice versa.
    The difference between static/non static in regards to enclosed classes is that the static ones are 'top-level' and cannot access the members of the enclosing class.
    The effect of making an enclosed class static means there is only one instance of any variables, no matter how many instances of the outer class are created. In this situation how could the static inner class know which variables to access of its non static outer class. Of course the answer is that it could not know, and thus an static inner class cannot access instance variables of its enclosing class.
    Now, regarding non-static inner classes, and trying to give a valid answer to the original post:
    As with instance methods and variables, a non-static inner class is associated with an instance of its enclosing class and has direct access to that object's instance variables and methods.
    TestOuter outer = new TestOuter();
    TestOuter.TestInner inner = outer.new TestInner();Because an inner class is associated with an instance (inner class implicitly keeps a reference to the object of the enclosing class that created it), it cannot define any static members itself. Static members cannot access the this pointer.
    So, in an ordinary (non-static) inner class, the link to the outer class object is achieved with a special this reference. A static inner class does not have that special this reference, nor would a static method/variable of an ordinary (non-static) inner class.

  • SerialVersionUID and inner classes

    Hello All
    A quick question about serialVersionUID in inner classes.
    If an inner class can't have any static members, how come it's legal to define
    static final long serialVersionUID = 123L;
    in an inner class ? Is there something special about serialization that allows this ?

    there is nothing special about the serialVersionUID defintion -
    you can define static datamember of inner class as long as it is final or the inner class is static :
    public class Out {
    public Out() {
    private class In {
    static final int i1 = 1; //legal
    static int i2 = 2; //** illegal
    private static class InStatic{
    static final int i1 = 1; //legal
    static int i2 = 2; //legal
    }

  • Canos EOS 60D: file access rights differ, dependant on mode of import

    I have stumbled upon the most stupefying and inexplicable inconsistency when importing photos from the Canon EOS 60D into iPhoto 09 via USB cable. I then move these files into a new empty folder on my desktop. Some files have access rights allocated to the user group "staff", and some do not. I discovered that pictures taken in portrait mode (camera rotated 90 degrees) have the "staff" access enabled, and those taken in horizontal mode do not. But this gets better still: when I take the very same Transcend 16 GB SDHC card and import these very same pics via the built-in SD slot and iPhoto on the iMac and move these pics to a new empty folder, ALL of them have "staff" access rights. Strange, right?
    Interestingly, this does not happen when I use a Canon EOS 20D. All pics get the "staff" group allocated with the 20D via USB.
    This is really bothersome, because I usually import via USB and move these pic folders to a 10.4 server. No "staff" means that the ACL´s don´t work properly, and I have to manually change access rights for other users.
    Anyone out there have an idea or an explanation for this? I really am at a loss this time.....
    Harald

    Haven´t tried image capture yet, will do so on Monday. Will also try copying files directly off of the card, into a new folder (bypassing iPhoto) to see what happens.
    Thing is that users on the network rely on the simplicity of the iPhoto import, which is why I am keen to find the cause of this anomaly. Any other ideas out there on what the cause might be?

  • Local inner classes

    Hi all,
    Local inner classes can access all instance / static variables (include private) from enclosing class & local variables / methods parameters declared as final.
    class Outer {
    private int a = 0;
    static int b = 1;
    public void localInnerClassTest(final int k) {
    final double x = 0.4;
    class LocalInner {
    public void test() {
    System.out.println(a + " " + b + " " + k + " " + x);
    new LocalInner().test();
    public static void main(String args[]) {
    new Outer().localInnerClassTest(3);
    // shows 0 1 3 0.4
    I�ve seen in some mock exams that�s wrong ...
    Thankz

    Local inner classes can access all instance / static
    variables (include private) from enclosing class &
    local variables / methods parameters declared as
    final.Yes. That is correct
    >
    I�ve seen in some mock exams that�s wrong ...You've seen that what's wrong? - the statement you made above or the code you posted?
    The code you posted should not compile (is that what you mean by "wrong"), but you have not explained the problem you are having...
    Do you have a question you would like answered?

Maybe you are looking for