1

How can we access Inner class methods or variables in salesforce from the anonymous window.Please refer the below example code:

public class OuterClass {
    public String CompanyName;
    public String CompanyAddress;
    public void PrintCompany(String CompanyName, String CompanyAddress){
        this.CompanyName = CompanyName;
        this.CompanyAddress = CompanyAddress;
        System.debug('Company name is : ' + CompanyName + '\n' + 'Company is located in : ' + CompanyAddress);
    }
        
    public class InnerClass{
        public String ModelName;
        public Integer Quantity;
        public Integer Price;
        public void Model(){
            System.debug('InnnerClass');
        }
    }
}

Anonymous window code:

OuterClass Comp = new OuterClass();
Comp.PrintCompany('Royal Enfield','Jodhpur');

So from the code I am not sure on how I can access the variables or methods in the anonymous window of the 'InnerClass' class? Also, I have given the access modifier to the inner class as public. As we cannot access private inner class outside the outer class and can be accessed inside the outer class only.

Another Question: -can we have public variables in inner class when it is defined private? As the variables cant be more restrictive than the class is my assumption correct?

1
  • Do note that anonymous apex cannot have inner classes. This is because everything in the anonymous apex window is basically its own "inner" class, and inner classes cannot have further inner classes defined inside of them. You can use other inner classes that already exist in your org, you just can't create any new (temporary) ones in anonymous apex.
    – Derek F
    Commented Dec 23, 2023 at 14:43

1 Answer 1

2

You use the notation of outer.inner, such as:

OuterClass.InnerClass inner = new OuterClass.InnerClass();
inner.ModelName = 'Acme';
// ... etc

Note that you must create an instance of an inner class, and use that instance to access inner class variables and methods. Static methods and variables are not allowed.

Methods will be called by using the variable, called an instance of the class.

inner.model();
3
  • Thanks a Lot for the quick response. and I have another question: can we have public variables in inner class when it is defined private? As the variables cant be more restrictive than the class is my assumption correct? Commented Dec 23, 2023 at 12:04
  • 1
    @SaiBhargav The compiler doesn't care what access level you ascribe to a variable. However, it works as you might expect: if you can't access the class, you can't access the variables in that class. For variables, there's no use case where a higher access level would be useful. Methods, on the other hand, may require higher access if implementing an interface, or subclassing an abstract or virtual class, because subclasses can't reduce the access level of an overridden method.
    – sfdcfox
    Commented Dec 23, 2023 at 13:11
  • 1
    Also worth mentioning that Flow can't natively access inner classes which becomes an issue if you are parsing callouts from JSON that generate inner classes Commented Dec 23, 2023 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .