You need to sign in to do that
Don't have an account?
sachin24
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
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.
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.
You instatiate the outer.inner class then you call the show method from your instance:
ex
outer.innerc oi = new outer.inner();
oi.show();
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...
This should be helpful
http://www.salesforce.com/us/developer/docs/apexcode/
Hi Sachin,
You can go with this code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay 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.
Thanks,
Ajay Dubedi
outer.inner ab=new outer.inner();
ab.show();