Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

http://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

share|improve this answer
    
Thank you Richard for the assistance. Its much appreciated. I understand your comments about indenting. I was having difficulty with the site's editor and couldn't seem to paste everything as a block of code, making it slow to just get my post entered. I'm headed straight to the links you've provided. :) –  crmprogdev Feb 7 '13 at 21:13
    
No problem :) I know posting code takes some getting used to. –  Richard N Feb 7 '13 at 21:14
    
@crmprogdev For future reference, the code editing here is just done with spaces when using large blocks of code like that. I edited the post for you to make the code more readable. Enjoy the site! –  Jesse Altman Feb 7 '13 at 21:17
    
Thanks for the tip Jesse! I'll catch on in time. –  crmprogdev Feb 7 '13 at 21:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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