0

i was trying to create an anon class in java

class myClass {

   int x = 10;

 }



public class Main  {
    public static void main(String[] args) {
        myClass myObject = new myClass() {

           public void run(){
               System.out.println(x);
           }
        };
        myObject.run(); //gives an error saying "method run() is undefined for the type myClass"
    }

  }

and it does not seem to work at all , it gives me an error saying that method run() is undefined although i created an anon class containing the run() mehtod , i am a complete java beginner , i code most of the time in javascript , javascript is the language i am most familiar with and i am trying to get used to java concepts so yeah this question might seem silly to alot of people here and i apologize for that. thanks in advance

AmirWG
  • 251
  • 1
  • 2
  • 10
  • Where is your `run()` prototype inside your interface declaration? – TaQuangTu Jul 28 '19 at 17:11
  • Provide your whole code if you want to get help. – mentallurg Jul 28 '19 at 17:12
  • 1
    It is not defined in `myClass` which is type of `myObject` variable. Just like `Animal animal = new Bird(); animal.fly();` wouldn't compile when `Animal` doesn't define `fly()` method, even if it will be defined in `Bird` class. – Pshemo Jul 28 '19 at 17:13
  • this is the whole code and i got my problem sovled in the answers below already so thanks for eveyone. – AmirWG Jul 28 '19 at 17:17

3 Answers3

5

This is because the compiler doesn't know there is a run method, because there isn't one on the base class myClass.

This would work in Java 10+, using var:

var myObject = new myClass() {

       public void run(){
           System.out.println(x);
       }
    };
    myObject.run();

This works because myObject isn't exactly a myClass, but is actually specifically the anonymous class, which is called something like TheContainingClass$1 (where TheContainingClass is the name of the class in which this code appears).

It's a bit weird because you can't actually refer to this class by name - only with var!

Or, in earlier versions of Java, it works if you don't assign it to a variable:

new myClass() {

       public void run(){
           System.out.println(x);
       }
    }.run();

Again, this works because the receiver of the run() call has the type of the anonymous class, not myClass; but there is just no way in pre-Java 10 to declare a variable of that type.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

The error occurs since your compiler is unaware of the method run()

You could fix it by making myClass an interface, or by using an abstract method.

abstract class myClass{
    int x = 10;
    public abstract void run();
}
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
1

If you use

interface myClass{
    public void run();
    int x = 10;
}

It should run. The problem here is that you don't have the run method in myClass declaration

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32