on
Italy
- Get link
- X
- Other Apps
class OuterClass { public static void main(String[] args) { System.out.println("Outer"); } class InnerClass { public void inner_print() { System.out.println("Inner"); } } }The output is just one line:
Outerand the file in bin directory should look like:
OuterClass.class OuterClass$InnerClass.classBut how do we get access to the method of inner class?
InnerClass inner = new InnerClass();Then, we invoke the method of inner class on the reference.
inner.inner_print();How about the following code?
class OuterClass { public static void main(String[] args) { System.out.println("Outer"); InnerClass inner = new InnerClass(); inner.inner_print(); } class InnerClass { public void inner_print() { System.out.println("Inner"); } } }
OuterClass o = new OuterClass(); InnerClass inner = o.new InnerClass();In other words, we're invoking a method on the outer instance. But the method is special in this case, and it happens to be instantiation method for an inner class.
class OuterClass { public static void main(String[] args) { System.out.println("Outer"); OuterClass o = new OuterClass(); InnerClass inner = o.new InnerClass(); inner.inner_print(); } class InnerClass { public void inner_print() { System.out.println("Inner"); } } }This time, the output is:
Outer Inner
class OuterClass { static class InnerClass { public void inner_print() { System.out.println("Inner"); } } }Note that we made the InnerClass as static.
public class StaticNestedClassTest { public static void main(String[] args) { System.out.println("Outer"); OuterClass.InnerClass inner = new OuterClass.InnerClass(); inner.inner_print(); } }Because a static nested class is static, we don't use an instance of the outer class. We just use the name of the class, the same way we invoke static methods or access static variables.
public class MyClass { public void myprint() { System.out.println("MyClass"); } } public class AnonymousTest { public static void main(String[] args) { MyClass myclass = new MyClass() { public void myprint() { System.out.println("Anonymous"); } }; myclass.myprint(); } }The output is:
AnonymousLet's look at what's in the code:
MyClass myclass = new MyClass() {is declaring a reference variable myclass. Then we declare a new class that has no name. But it's a subclass of MyClass.
MyClass myclass = new MyClass() { public void myprint() { System.out.println("Anonymous"); } public void myprint2() { System.out.println("Anonymous2"); } }; myclass.myprint(); myclass.myprint2();We'll have following error:
The method myprint2() is undefined for the type MyClass.Why?
interface MyInterfaceable { public void myprint(); } public class AnonymousTest { public static void main(String[] args) { MyInterfaceable myinterface = new MyInterfaceable() { public void myprint() { System.out.println("Anonymous"); } }; myinterface.myprint(); } }What's going on in the line:
MyInterfaceable myinterface = new MyInterfaceable() {It creates an instance of an anonymous inner class, an implementer of the MyInterface interface. Notice that we have
new MyInterfaceable()even though MyInterface is an interface and so we can't make an instance of it. But this syntax is saying, "create a new class (with no name) that implements the MyInterface interface, and we have the implementation of the interface methods: myprint(). Compare the following two cases:
MyInterfaceable myinterface = new MyInterfaceable();
MyInterfaceable myinterface = new MyInterfaceable() { public void myprint() {} };
public class AnonymousTest { public static void main(String[] args) { CookableHandle handle = new CookableHandle(); handle.useCookableInterface(new MyCookable() { public void mycook() { System.out.println( "Cooking Anonymous Arg Inner Class"); } // end of mycook() } // end of inner class ); // end of argument } } public class CookableHandle { void useCookableInterface(MyCookable f) { f.mycook(); } } interface MyCookable { public void mycook(); }The output is:
Cooking Anonymous Arg Inner ClassLet's look at what's going on the line:
handle.useCookableInterface(new MyCookable() {We call useCookableInterface() on a CookableHandle object. However, the method takes an instance which is an interface MyCookable. That's why we have to make both an implementation class and an instance of that class.
new MyCookable() {It defines the new class for the anonymous class that implements the MyCookable interface. MyCookable interface has one method to implement, mycook(). So, we implement the mycook() method.
Comments
Post a Comment