0
package innerclasstest;

interface Demo {
}

class Bar {

    public void call() {
    Foo f = new Foo();

    f.doStuff(new Demo() {

        public void fall() {
            System.out.println("In method args...");
        }

    });
}

}

class Foo {

public void doStuff(Demo demo) {

System.out.println("In stuff");
}
}

public class ClassArg {

public static void main(String[] args) {
    Bar b = new Bar();

    b.call();

}

}

In the above example how we can call the anonymous class method Fall. in there is any way to call this method.I don't know which approach I should pick to call this method.

1
  • You have discovered one of the several reasons that anonymous classes should be, IMO, "considered harmful". They can't be reused and often lead to a ton of copy and pasted code. YMMV. Commented Dec 10, 2013 at 17:18

4 Answers 4

4

The only reason you cannot write

demo.fall();

is that you didn't announce that method in the interface. Change it like:

interface Demo {
    void fall();
}

and then it works.

Sign up to request clarification or add additional context in comments.

Comments

2

If you want to do different things with an annonymous class you need to assign it to a variable, as the class is not reusable.

Something like:

Demo extendedDemo = new Demo() {

    public void fall() {
        System.out.println("In method args...");
    }

};

you can use it then for your call:

f.doStuff(extendedDemo);

You can call the internal method at declaration if doStuff can be executed after:

Demo extendedDemo = new Demo() {

    public void fall() {
        System.out.println("In method args...");
    }

}.fall();

If you can't call doStuff later you can call the internal method using reflection:

Method m = extendedDemo.getClass().getMethod("fall", new Class[]{}); 
m.invoke(extendedDemo, new Class[]{});

Comments

1

Anonymous classes can't be referred anywhere other than the place they have been declared. To call the method it has to be declared in the interface first. Here is your complete listing with the interface tweaked a bit to make use of the method:

interface Demo {
    public void fall();
}

class Bar {
    public void call() {
        Foo f = new Foo();
        f.doStuff(new Demo() {
            public void fall() {
                System.out.println("In method args...");
            }
        });
    }
}

class Foo {
    public void doStuff(Demo demo) {
        System.out.println("In stuff");
        demo.fall();
    }
}

public class ClassArg {
    public static void main(String[] args) {
        Bar b = new Bar();
        b.call();
    }
}

The output will be:

In stuff
In method args...

See http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Comments

0

In your situation :

public void doStuff(Demo demo) {

System.out.println("In stuff");
}

in this method call demo.fall() if the Demo interface provides the fall() method declaration.

1 Comment

the problem is that the Demo interface does not have a fall() method declared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.