-3

I am working on anonymous classes and while working i come up with the case where i am unable to call a method using anonymous class.

I am getting following compilation error at m1()

The method m1(int) in the type new I(){} is not applicable for the arguments ()

 interface I {
        public void m1(int arg1);
    }

    class A {
        static void m2(I i) {
        }
    }

    class B {

        class C {
            void m4() {
                A.m2(new I() {
                    public void m1(int arg1) {
                        m1();// Getting compilation error here.
                    }
                });
            }

            void m1() {
                System.out.println("Inside M1");
            } 

        }
    }

Can some one help me understand, why i am getting this error ? How to fix it

For those who does not understand the code please find the attached screen shot.

T-Bag
  • 10,916
  • 3
  • 54
  • 118
  • seems you want to call `void m1()` stated at the bottom of the class `C` but you are doing a recursive call to same `m1(int arg1)` method which is taking an argument. So either you rename your `m1` method at the bottom or you add some `int args` in the call! – R Dhaval Jun 20 '17 at 09:43
  • 1
    Please format this incomprehensible mess properly. It is impossible to determine scope from what you have posted. – user207421 Jun 20 '17 at 09:44
  • why dont you just rename the ***void m1()*** to something less conflictive? – ΦXocę 웃 Пepeúpa ツ Jun 20 '17 at 09:44
  • I have pasted the exact eclipse screen shot. Yes i can rename and continue but i want to understand what is happening here, that is why i posted this question. – T-Bag Jun 20 '17 at 09:47
  • Your 'exact screen shot' is no better formatted than your original posting. Please fix this. Or don't expect an answer. It's up to you. `class A` and `class B` are on the same lexical level as `interface I` in your code, and should therefore have the same indentation: and similarly throughout. – user207421 Jun 20 '17 at 09:56
  • @EJP- I already got the answer, those who are willing to answer they are not concerned about blank spaces, i guess its the logic that matters not the cosmetic changes. – T-Bag Jun 20 '17 at 10:00
  • @ΦXocę웃Пepeúpaツ- Thanks - very nicely explained. – T-Bag Jun 20 '17 at 10:01
  • Everybody in computer programming is concerned about blank spaces. They are critical in expressing the meaning of the code. You may not be interested in that, which is one thing, but when you post your code to be inspected by millions of others, that is another thing entirely. As I said, it's up to you. – user207421 Jun 20 '17 at 10:05
  • @EJP-- Thanks for you kind suggestion i will surly take care next time.. :) – T-Bag Jun 20 '17 at 10:12

3 Answers3

2

If you want to call the m1() method in C - the only m1 method taking no parameters - from the anonymous class, you have to qualify this:

C.this.m1();
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

The interface called I has no method m1() without arguments.

so this method :

public void m1(int arg1) {
    m1();// Getting compilation error here.
}

tries to call a non existing method.

Note that m1() is equivalent to this.m1(). The this is an instance of your anonymous inner class, and not of the outer C class.

To call the m1 method of the outer class you need to do :

C.this.m1();

See a full explanation here under the Shadowing header.

bowmore
  • 10,842
  • 1
  • 35
  • 43
1

You have 3 options in it:

renaming the void m1() method will be a way to avoid to shoot yourself in the foot!

 class B {

    class C {
        void m4() {
            A.m2(new I() {
                public void m1(int arg1) {
                    m1Foo();// Getting compilation error here.
                }
            });
        }

        void m1Foo() {
            System.out.println("Inside M1");
        } 

    }
}

or if you can not/ dont want then turn that into a lambda

class B {

    class C {
        void m4() {
            A.m2(arg1 -> m1());
        }
        void m1() {
            System.out.println("Inside M1");
        }
    }
}

or qualify the nethod, so the compiler can know which m1 are you meaning

class B {

    class C {
        void m4() {
            A.m2(new I() {
                public void m1(int arg1) {
                    C.this.m1(); 
                }
            });
        }

        void m1() {
            System.out.println("Inside M1");
        } 

    }
}
T-Bag
  • 10,916
  • 3
  • 54
  • 118
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97