javaanonymous-inner-class

How to get access to the own methods of anonymous inner class


Can anybody help to optimize the mess in my head produces the following stupid code snippet?

public class SomeClass {

    interface MethodInvokeHelper{
        void methA(int funNum, Object ... v);
        <T> T methB(int funNum, Object ... v);
    }

    static class SomeInnerClass{
        void someMethod(){System.out.println("SomeInnerClass method");}
    }

    public static void main(String[] args) {
        MethodInvokeHelper anonymous = new SomeInnerClass(){

            void methAnonym1(){ System.out.println("methAnonym1"); }

            void methAnonym2(){ System.out.println("methAnonym2"); }

            Integer methAnonym3(){ return 10;}

            String methAnonym4(){return "methAnonym4";}

            Integer methAnonym5(Object ... v){
                    int sum = 0;
                    for (Object i : v)
                            if(i instanceof Integer)sum += (int) i;
                            else return 0;
                    return sum;
            }

            MethodInvokeHelper refs(){

                    return new MethodInvokeHelper(){
                        public void methA(int funNum, Object ... v) {
                            switch(funNum){
                                case 1 -> methAnonym1();
                                case 2 -> methAnonym2();
                            }
                        }
                        public Object methB(int funNum, Object ... v) {
                                return switch(funNum){
                                        case 1 -> methAnonym3();
                                        case 2 -> methAnonym4();
                                        case 3 -> {
                                                if(v.length>0) yield methAnonym5(v);
                                                else yield null;
                                        }
                                        default -> null;
                                };
                        }
                    };}
            }.refs();

        anonymous.methA(1);
        System.out.println((Object) anonymous.methB(1));
        System.out.println((Object) anonymous.methB(3,3,4));
    }

I just want to bypass the constraints of use the own methods of anonymous inner class inside outer code.

Update: by @Old Dog Programmer comment the names of classes have been changed.

Update to clarify by propose of @Sweeper: So let specify the problem that inducted the question above:

In the following code:

public class Outer {
        Outer(){}
        void method() {
                System.out.println("the method of Outer");
        }
    
        public static void main(String[] args){
                Outer outter = new Outer() {
                        { super.method(); method(); nmethod(); }
                        void method(){
                                System.out.println("method() from anonymous class");
                        }
                        void nmethod() {
                                System.out.println("nmethod() from anonymous class");
                        }
                };
                outter.method(); // we can call the overriden method
                // outter.nmethod(); //but we can not call the own method of anonymous inner class from outside
        }
    }

So, I want to find a possibility to call outter.nmethod();


Solution

  • If I do understand the question correctly, it can be done using var (Java 10 or later) to declare the variable like in:

    var outter = new Outer() {  // or just new Object()
        void nmethod() {
            System.out.println("OK");
        }
        void omethod() {
            System.out.println("Another method");
        }
    };
    
    outter.nmethod();
    outter.omethod();
    

    It is like the compiler knowns the type of the anonymous class.