I am bit confused on accessing the inner class variable from a different. I have created a class structure to serialize and create a json request. I need to send an custom object with attachment.

public class outerclass {
public string Name;
public string Department;
public list<innerclass> incls;

public class inneClass {
     public string attachmentbody;
     public string contentType;
 } }

Now , I need to intialize the variable. I can easily create the instance of the outer class and assign the value. but , how should I do it for inner class. I am doing it in a third class.

public thirdClass {
function (){
   outerClass OC = new Outerclass ();
   OC.name =  'John';
   OC.Department = 'sales';
   OC.incls = ?? 

How can I initialize this list, add multiple instance for inner class. It's like John sales records has mulitple attachment, but how can I have multiple instance for inner class and attach it to outer class instance. thanks.

  • hi .. Can someone help me with this ? Do i need to custom controller for the inner class to initialize it's member. – amidstCloud Feb 4 '15 at 0:41
  • OC.incls = new List<outerclass.innerClass> (); followed by lines such as OC.incls.add(new OuterClass.innerClass(..constructor args..)); Your innerclass needs a constructor or setters – cropredy Feb 4 '15 at 2:12

I m not sure if I misunderstood your question. to initialisation the inner class collection you have, it will be same as you create any other list and add element into it..

public thirdClass {
function (){
   outerClass OC = new Outerclass ();
   OC.name =  'John';
   OC.Department = 'sales';
   List<Outerclass.innerClass> incls = new List<Outerclass.innerClass>();
   for(Iterate your JSON array){
       Outerclass.innerClass inc = new Outerclass.innerClass();
       inc.attachmentbody = '';
       inc.contentType = '';
   oc.incls = incls;  

this is what i tried in my dev org which works

Class 1 :

public class OuterClass {

    public Integer outerInt;
    public List<innerClass> icList;

    public class innerClass{
        public String testMe;

Class 2 :

public class hello {

    public string testInnerClass(){
        OuterClass oc = new OuterClass();
        oc.outerInt = 1;
        oc.icList = new List<OuterClass.innerClass>();
        OuterClass.innerClass ic = new OuterClass.innerClass();
        ic.testMe = 'test';
        return 'success';
| improve this answer | |
  • We can't initialize inner class like this. ist<innerClass> incls = new List<innerClass>() & innerClass inc = new innerClass() won't work.. Try it yourself ... – amidstCloud Feb 4 '15 at 0:11
  • not sure what you are doing, but wen i tested, it works fine. only thing i missed is when you access the innerClass, you have to go thu the outerClass – Vamsi Krishna Feb 4 '15 at 0:51
  • Go through the outer class means.. write innerClass inc = new innerClass() in outer class ..correct...Yeah ..thats true. in Outer class we can write./access innerclass inc = new innerClass.. but I have to access it from the third class. – amidstCloud Feb 4 '15 at 0:56
  • I hv updated the code that i tested in my dev org.. i dont get any errors. can u let me know wat error you get when u use this code or your code ? – Vamsi Krishna Feb 4 '15 at 1:14
  • Thanks Vamsi. Although , I have tried the same approach earlier , It didn't work.. but your post made me give it try again . and it worked.. Thanks a lot – amidstCloud Feb 4 '15 at 3:02

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.