function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sachin24sachin24 

how to call inner class in Apex..

Hi,

i want to call inner class member in Apex , A very simple code i was trying to run:

 

public class Outer

{

  class Inner

    {

      public void show()

        {

             System.debug('Hello');

        }

   }

}

When i tries to call the show() of inner class in developer console with the below statement:

new Outer().new Inner().show();

Then after clicking execute button i m getting unexpected token 'new' error.

however the above code is working in fine in Java, i m new to apex, could you help me please

vbsvbs
Try this:
new Outer.Inner().show()

Make sure to define the inner class as public to allow access. Note: Both Outer and Inner are reserved words in apex.
sachin24sachin24

could you please provide me the detailed explanation of why the inner method were not called with the help of below syntax:

new Outer().new Inner().show();

and why it worked in java not in apex.

ForceNoobForceNoob

You instatiate the outer.inner class then you call the show method from your instance:
ex

        outer.innerc oi = new outer.inner();
            oi.show();

 

sachin24sachin24

Hi guys,

the soultion that you are giving is the solution of static nested class in java,

so can we assume that by default the access modifiers of nested class in apex is static nested class instead of non-static nested class???

 

kindly find the below code which illustrates the example of static nested class in java:

public class A
{
static class B
{
public void show()
{
System.out.println("Hello");
}
}

public static void main(String[] ar)
{
A.B ab = new A.B();
ab.show();
}
}

 

in the above code you can see the resoultion provide by you that is: 

outer.inner oi = new outer.inner();

oi.show();

works same as that of static nested class in Java.

however, if in apex by default the inner class is not the static one then no doubt as per the object oriented principle we can call the inner method using below syntax:

i.e Outer.Inner oi = new Outer().new Inner();

     oi.show(); // calling of Inner class method.

 

can any one throw detailed analysis not a workaround...

ForceNoobForceNoob
Force.com Apex syntax is not exactly the same as JAVA.
This should be helpful
http://www.salesforce.com/us/developer/docs/apexcode/
vbsvbs
@Sachin - You are making far too many assumption here. Although the syntax of Apex is Java like and the underlying code execution is based on a JVM this is not a like-for-like case. In Apex there are no calss modifiers applicable on inner classes. Hence you can access inner class like static classes and still instantiate objects. In addition, only one level of class nesting is allowed unlike Java. If this does help you do mark this as a solution so other can refer to it.
Ajay K DubediAjay K Dubedi

Hi Sachin,
   You can go with this code:
   
Outer.Inner innerObj = new Outer.Inner();
            innerObj.show();

   I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
   Thanks,
   Ajay Dubedi
Ajay K DubediAjay K Dubedi

Hi Sachin,
  I think you are calling nested call from its outer class so the following code will be help you. It's working for me.
 
public class NestedClassCall{
    class InnerClass{
     public void show() {
      System.debug('Hello');
     }
    }
    public void showMessage(){
     InnerClass innObj = new InnerClass();
     innObj.show();
    }
   }

   
   Thanks,
   Ajay Dubedi
manu manu 23manu manu 23
why don't you try the syntax like this?
outer.inner ab=new outer.inner();
ab.show();