Structure for classes to call inner or sub classes

I don’t have a Java background and am working on a class where I’d like to be able to call inner classes from withing methods of the outer class. As much of Apex is implicit and I’m still learning, I sometimes find the proper syntax for things like this a bit confusing.

As I understand it, all methods in a class are executed sequentially and there’s no simple way to call one from another. Am I correct on that? If so, the solution seems to have an outer class that could call inner classes ass needed causing only the necessary code to execute.

Exactly how would I set up the call outs for that kind of class? Here’s some pseudo-code for what I’m trying to accomplish and how I think the classes and methods should be arranged:

public class myNestedClssUtil {
    public class myNestedClssUtilEntry {
        public string MyStrng;
        public processMyStrng() {
            // code to process MyStrng
            // Decision Tree
            if (MyString, Condition A) {
                call to MyNestedClss1(MyStrng);
                Return MyStrng;
            } else {
                call to MyNestedClss2(MyStrng);
                Return MyString;
            }
        }
        private class MyNestedClss1 {
            private class MyNestedClss1Entry {
                private string inStrng1;
                private procInStrng1() {
                    //code to process inStrng1
                    Return inStrng1;
                 }
            }
        }
        private class MyNestedClss2 {
            private class MyNestedClss2Entry {
                private string inStrng2;
                private procInStrng2() {
                     //code to process inStrng2
                     Return inStrng2;
                }
            }
        }
    }
}

I should add that this is for a class where a string is passed into the class, processed and then returned. I’ve not shown a constructor anywhere above. This is intended as a utility class for use inside of a trigger. Thank you for your assistance and input on how to do this using the correct syntax.

Answer

It will be great if you can indent your code for better readability.

And of course you can nest classes within each other.

You would need to instantiate the child class and use the object to access its methods.

Some sample code:

public OuterClass
{
    //Declarations and methods.

    //Here is how you would access the child class.

    InnerClass obj1 = new InnerClass();

    //Invoke method
    obj1.doSomething();

    public InnerClass
    {
        public String Name {get; set;}

         //constructor
         public InnerClass()
         {
         }

        //Method
        public String doSomething()
        {
        }

    }
}

Some links to help you.

https://stackoverflow.com/questions/11763874/what-is-the-proper-method-include-an-inner-class-in-apex

http://forceschool.blogspot.com/2011/06/inner-class-in-apex-class.html

Attribution
Source : Link , Question Author : crmprogdev , Answer Author : Community

Leave a Comment