-7

I have the following case. now I need to call method Two from an other class, how do I do it? In java you can declare a class inside a method.

public class Name {
    void methodOne() {
        class InnerClass {
           void methodTwo() {
           }
         }
     }
 }
Learner
  • 425
  • 5
  • 14

1 Answers1

2

I have tested this in Java and prints the message:

class Foo {
    void bar() {
        class Baz {
            void hi() {
                System.out.println("Hi");
            }
        }
        Baz baz = new Baz();
        baz.hi();
    }
}

For non-Java programmers, this would result very odd, but is the base for anonymous classes

now I need to call methodTwo from another class

Since the Baz class is inside the bar method, you can't use it outside this method. The only case when you can do that is when this Bar class implements a public interface (or extends a public [abstract]class) that can be consumed by the another class. For example:

interface Polite {
    void hi();
}

class Bud {
    void aMethod(Polite polite) {
        polite.hi();
    }
}

class Foo {
    void bar() {
        class Baz implements Polite {
            @Override
            public void hi() {
                System.out.println("Hi");
            }
        }
        Polite baz = new Baz();
        Bud bud = new Bud();
        bud.aMethod(baz);
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 1
    Except the OP wants to "call methodTwo from an other class", which isn't possible, unless of course that other class is also declared in the same method. – Paul Bellora Apr 26 '13 at 21:44
  • 1
    Baz is a "member" (local "variable" of type Class might be more accurate, although the funny thing is the "variable" doesn't get assigned a name) of the _method_ (Bar), not of the _class_ (Foo). Hence the inner class is visible only inside the method, I presume. – Erwin Smout Apr 26 '13 at 21:45
  • 1
    @PaulBellora answer updated trying to give some light. – Luiggi Mendoza Apr 26 '13 at 21:52
  • Inner hi method has to be public to make it work.. thanks – Learner Apr 26 '13 at 22:19
  • @Learner code fixed. Sorry, was just typing the code on answer. – Luiggi Mendoza Apr 26 '13 at 22:23
  • @LuiggiMendoza thanks. How do we initialize interface in methos aMethod and what is the use of Polite baz = new Baz(); Bud bud = new Bud(); bud.aMethod(baz); – Learner Apr 26 '13 at 22:41
  • I would like to call the interface form the aMethod() – Learner Apr 26 '13 at 22:42
  • @Learner as you can see, you receive an instance of the `Polite` interface in the `aMethod` so you just have to initialize it outside. In this case, the implementation of this interface is done by the `Baz` class inside the `Foo#bar` method. – Luiggi Mendoza Apr 26 '13 at 22:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29012/discussion-between-learner-and-luiggi-mendoza) – Learner Apr 26 '13 at 22:44
  • It would be possible to call the method elsewhere if 1) the inner class instance was returned and 2) it implemented an interface visible to the callee. – Sled Nov 06 '14 at 17:37
  • @ArtB yes, that's covered in the second code example. – Luiggi Mendoza Nov 06 '14 at 17:41