3

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.

Raghav Dinesh
  • 591
  • 5
  • 12
Jeetendra Ahuja
  • 177
  • 2
  • 17

3 Answers3

4

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().

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Yaah I was able to figure that if I assign my anonymous class to List or LinkedList variable then I can call up methods of LinkedList but not dummy1() and dummy2()... I was just wondering if there are some other ways. Thanks Sir for instant help. – Jeetendra Ahuja Jan 21 '18 at 15:17
  • @JeetendraAhuja Well, you can assign the anonymous class instance to a variable and then find and call the existing methods via reflection. That's the only way I can think of. – Eran Jan 21 '18 at 15:19
  • @anurag just mentioned that :) Thanks again – Jeetendra Ahuja Jan 21 '18 at 15:24
2

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();
Anurag Sharma
  • 2,409
  • 2
  • 16
  • 34
1

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.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
  • I understand your rationale but I was just trying to play with it and figuring if we can do so. I like your way of defining local classes and will use it in future if I come across this requirement. For now, I got to know that Reflection can allow it do so. Thanks Sir for help. – Jeetendra Ahuja Jan 21 '18 at 15:40
  • Can you please explain your last line with some example - "This is very useful.................. space", I tried to understand with few examples but don't understand which scenario you are referring to exactly. – – Jeetendra Ahuja Feb 01 '18 at 01:19
  • 1
    Any time you need bunch of recursive methods which you definitely want to use only once inside a method, or when you want a bunch of little helper classes for some small data structure which you want only once inside a method, you can use that. It's just when you are too lazy setting up 3 class files or a separate package for one stupid method, you can just define the necessary classes for the data structure inside the method itself. I can't come up with a convincing example right away, to be honest... In Scala it's trivial, so I don't think about it too often... – Andrey Tyukin Feb 01 '18 at 02:01
  • @JeetendraAhuja Here is an example: https://stackoverflow.com/questions/48563933/regex-that-matches-only-if-a-string-contains-a-word-from-each-list/48565767#48565767 Here, I wanted to enumerate some weird permutations of strings, but didn't want to create a separate recursive method for that. So I moved in into a local PermutationHelper. – Andrey Tyukin Feb 01 '18 at 15:12
  • Ah, I understand it now. Thanks @AndreyTyukin for detailed explaination and the link. :) +1 – Jeetendra Ahuja Feb 02 '18 at 18:00