|
|
| Brad Foster 2004-05-18, 2:33 pm |
| How can a static method in the outer class access a method in its inner
class?
| |
| VisionSet 2004-05-18, 7:32 pm |
|
"Brad Foster" <brad_foster12@hotmail.com> wrote in message
news:c8dh98$tvv$1@mailgate2.lexis-nexis.com...
> How can a static method in the outer class access a method in its inner
> class?
OuterClass.InnerClass inny = new OuterClass().new InnerClass();
inny.myInnerMethod();
--
Mike W
| |
| Yu SONG 2004-05-19, 11:32 am |
| VisionSet wrote:
> "Brad Foster" <brad_foster12@hotmail.com> wrote in message
> news:c8dh98$tvv$1@mailgate2.lexis-nexis.com...
>
>
>
> OuterClass.InnerClass inny = new OuterClass().new InnerClass();
> inny.myInnerMethod();
>
> --
> Mike W
>
>
Or write some methods in the OuterClass
--
Song
More info.:
http://www.dcs.warwick.ac.uk/~esubbn/
| |
|
|
"Brad Foster" <brad_foster12@hotmail.com> wrote in message
news:c8dh98$tvv$1@mailgate2.lexis-nexis.com...
> How can a static method in the outer class access a method in its
inner
> class?
Depends.
If inner class is static and inner method is static:
StaticInnerClass.staticMethod();
If inner class is static and inner method is not static, you need an
object of inner class:
new StaticInnerClass().nonStaticMethos();
If inner class is not static and inner method is not static, you need
an object of outer and inner class:
new OuterClass().new InnerClass().nonStaticMethod();
Non static inner classes cannot have static declarations,
so it's impossible to call a static method of a non static
inner classes.
<code>
public class InnerTest
{
public static void main(String[] args)//static method of outer class
{
StaticInner.staticMethod();
new StaticInner().nonStaticMethod();
new InnerTest().new Inner().nonStaticMethod();
}
static class StaticInner
{
static void staticMethod()
{
System.out.println("StaticInner.staticMethod()");
}
void nonStaticMethod()
{
System.out.println("StaticInner.nonStaticMethod()");
}
}
class Inner
{
//won't copile
//static void
staticMethod(){System.out.println("Inner.staticMethod()");}
void nonStaticMethod()
{
System.out.println("Inner.nonStaticMethod()");
}
}
}
</code>
Adam
|
|
|
|