0

When I try to call this from execute anonymous it fails and says "Type is not visible: OutterClass.InnerClass "

OutterClass.InnerClass inresponse = new OutterClass.InnerClass(); System.debug(inresponse.name);

public class OutterClass {
    Public String name = 'outterclass';
    
    public void setName(String name){
        this.name = name;
    }
    
    class InnerClass{
        String name = 'innerclass';
    }

}

Please help

3

Since you didn't specify an access modifier for the inner class, the default one (private) has been used, as stated in the documentation:

The private access modifier declares that this class is only known locally, that is, only by this section of code. This is the default access for inner classes—that is, if you don't specify an access modifier for an inner class, it is considered private. This keyword can only be used with inner classes (or with top level test classes marked with the @isTest annotation).

In order to create an instance of the inner class you have to declare it public.
Just change class InnerClass { to public class InnerClass {.

By the way, also the name property is not accesible, so you have to declare it public too.

public class OutterClass {
    Public String name = 'outterclass';
    
    public void setName(String name) {
        this.name = name;
    }
    
    public class InnerClass{
        public String name = 'innerclass';
    }
}

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.