1

Create Class

public class OuterClass {
    protected Integer i = 0 ;
    public class InnerClass {
        public InnerClass() { 
            i = 1;
        }
    }
}

Unable to save class which should be saved as per the below doc:-

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm

1
  • You can't access instance variables like that, ony static variables.
    – Adrian Larson
    Nov 5 '15 at 18:42
3

Here is the documentation for this :

An inner class behaves like a static Java inner class, but doesn’t require the static keyword. An inner class can have instance member variables like an outer class, but there is no implicit pointer to an instance of the outer class (using the this keyword).

Static class in apex

so in your case if you want to access value of outer class you can create instance of outerclass and get the value

Public Class OuterClass
{

  Protected Integer i = 0 ;

 Public Class InnerClass
{
  public integer intinner;
  Public InnerClass()
  { 
       outerclass obj = new outerclass();
       intinner = obj.i;
       system.debug('*****************'+ intinner);
   }
 }

}
1
  • Might be worth adding that if OP wants to access i from the parent, the variable either needs to be static or they need to pass the parent into the children.
    – dphil
    Nov 5 '15 at 18:42
0

Given the way inner classes work in Apex (as Himanshu has explained) a couple of common patterns are:

// Copy a value on construction of the inner class
public Class OuterClass {
    private Integer i = 0;
    public Class InnerClass {
        Integer i;
        public InnerClass(Integer i) { 
            this.i = i;
        }
        ...
    }
    ...
        InnerClass inner = new Inner(i);
    ...
}

which means there are separate copies of the values or:

// Copy a reference on construction of the inner class
public Class OuterClass {
    private Mutable m = new Mutable();
    public Class InnerClass {
        Mutable m ;
        public InnerClass(Mutable m) { 
            this.m = m;
        }
        ...
    }
    ...
        InnerClass = new InnerClass(m);
    ...
}

which means changes to the referenced object made via the inner or outer classes are also seen in the outer or inner class respectively.

A version of the latter pattern that approximates a Java non-static inner class is:

// Copy a reference to the outer class on construction of the inner class
public Class OuterClass {
    public Class InnerClass {
        OuterClass outer;
        public InnerClass(OuterClass outer) { 
            this.outer = outer;
        }
        ...
    }
    ...
        InnerClass inner = new InnerClass(this);
    ...
}

where the inner class just needs to qualify its references to the outer class with an outer. prefix.

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.