5
public class Foo {
    public foo() { }
}
public class Outer {
    public Foo foo {get; set;}
    public class Inner {
        public Inner() {
            system.debug(foo);
        }
    }
    public Inner inner {get; set;}
    public Outer() {
        foo = new Foo();
        inner = new Inner();
    }   
}

That code would result in the following error:

Error: Compile Error: Variable does not exist: foo at line ....

How can my inner class access its outer class's members?

Thanks!

3

One option would be to pass an instance of the Outer class to the Inner class constructor and keep it in a member.

public class Foo {
    public foo() { }
}
public class Outer {
    public Foo foo {get; set;}
    public class Inner {
        private Outer o;
        public Inner(Outer out) {
            o = out;
            system.debug(o.foo);
        }
    }
    public Inner inner {get; set;}
    public Outer() {
        foo = new Foo();
        inner = new Inner(this);
    }   
}

Alternatively, you could make foo static and reference it with Outer.foo.

  • This is what I have been doing in all other circumstances where this has come up, but a class like Outer (or Foo) will be passed by value and not by reference. In this case, my Outer (and Foo) is a gigantic class which I don't want to copy 50 times. Let me give the static a try.. – Matthew Moisen Jun 10 '13 at 23:00
  • 1
    Passing this from outer to inner is your best bet. In Java you can leverage the implicit pointer that exists between the inner and outer class instances with syntax from within the Inner instance like Outer.this.foo but SFDC has removed access to this implicit pointer. Per the docs on the differences between Java and Apex: "Inner classes can have instance member variables like outer classes, but there is no implicit pointer to an instance of the outer class (using the this keyword)." – Mark Pond Jun 10 '13 at 23:22
  • @Daniel Ballinger this is great, I have been doing the same myself. But a question arises, is this the correct way to hard wire classes together? As this will explode the view-state. What alternatives have you used, is there something apart from this the community recommends? Or is depending on VF to build such complex UI is the right approach? – Mohd Nadeem Shaikh Feb 12 '16 at 7:08
  • @ShaikhMohdNadeem I haven't had problems with viewstate with this approach yet. Nor has it been a problem with String serialization. Probably best to test your specific scenario though with either of the objects has a large amount of data. If there is something you don't want in viewstate use the transient keyword. – Daniel Ballinger Feb 14 '16 at 7:41
  • Thank you @DanielBallinger! I'll play a little more with transient. Thanks for pointing it out :) – Mohd Nadeem Shaikh Feb 15 '16 at 5:01
0

Pass the params through the Inner Class constructor for sure.

Or completely in a different way, it's to use 'extends' and 'super' : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_super.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.