Jeetendra Ahuja
Jeetendra Ahuja

Reputation: 177

How to call multiple methods of Anonymous class?

In below chunk of code, I am creating an anonymous class by extending LinkedList but don't know how can I call-up multiple methods outside the anonymous class. I am able to call one method though as mentioned in end by .dummy1()

void methodOC_3() {

    int x = 0;
    new LinkedList() {
        /**
         * 
         */
        private static final long serialVersionUID = -2091001803840835817L;

        // Anonymous class is extending class LinkedList
        public void dummy1() {
            //x=4; Will give error, Or declare x as final variable
            //you can read it As Of Now, since it is effectively final.
            System.out.println(x);
        }

        @SuppressWarnings("unused")
        public void dummy2() {
            dummy1();
            System.out.println("Hey there :) ");
        }
    }.dummy1(); 
}

I have just started exploring anonymous classes and inner classes. Please let me know if I am missing anything.

Upvotes: 3

Views: 1713

Answers (3)

Andrey Tyukin
Andrey Tyukin

Reputation: 44918

Why exactly do you want your class anonymous? If it's anonymous, you can't refer to it, and this is exactly your problem. So, just don't make it anonymous! You can define local classes within methods:

public static void methodOC_3() {

    int x = 0;
    class MyList<X> extends java.util.LinkedList<X> {
        /**
         * 
         */
        private static final long serialVersionUID = -2091001803840835817L;

        public void dummy1() {
            //x=4; Will give error, Or declare x as final variable
            //you can read it As Of Now, since it is effectively final.
            System.out.println(x);
        }

        public void dummy2() {
            dummy1();
            System.out.println("Hey there :) ");
        }
    }

    MyList<String> a = new MyList<String>();
    a.dummy1();
    a.dummy2();
}

This is very useful especially if you want to define multiple mutually recursive helper methods inside another method, without polluting the name space.

Upvotes: 1

Anurag Sharma
Anurag Sharma

Reputation: 2605

Only way to call anonymous class's method is by using reflection with reference variable 'linkedList'

LinkedList linkedList = new LinkedList() { ... }

linkedList.getClass().getMethod("dummy1").invoke();

Upvotes: 2

Eran
Eran

Reputation: 393781

You can't.

The only way to be able to call multiple methods is to assign the anonymous class instance to some variable.

However, in your LinkedList sub-class example you can only assign the anonymous class instance to a LinkedList variable, which will only allow you to call methods of the LinkedList class.

If your anonymous class instance implemented some interface that has dummy1() and dummy2() methods, you could assign that instance to a variable of that interface type, which would allow you to call both dummy1() and dummy2().

Upvotes: 4

Related Questions