Interface can only be defined in a top-level class or static context

Hi, I hope this question isn't too obvious or anything. Whenever I try to create an interface, like below, I always get an error that says:

interface TheInterface { void move(); }

The member TheInterface can only be defined inside a top-level class or static context.

What am I doing wrong?

Thanks!

Answers

  • Answer ✓

    Either use

    static interface TheInterface {
      void move();
    }
    

    or my preference would be to create a new tab called TheInterface.java and put the interface code in that

    TheInterface.java tab

    interface TheInterface {
      void move();
    }
    
  • BTW I think the first version (static) would work but I only ever create interfaces in their own tab so I haven't tested it.

  • edited September 2017

    https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    • By default, when we create classes in Processing's IDE (PDE), they're inner, not top-classes! @-)
    • Inner classes can't have static members defined in their body. [-X
    • Unless their final compile-known constant fields. :-$
    • In Java however, all interfaces are implicitly static. ~O)
    • As a workaround, you can move your interface member outside its current inner class. *-:)
    • Or if you can, explicitly declare that nested class as static, so it can have static members. :ar!
  • Thank you for the responses! Creating it in a new tab did the trick :) Have a good one <3

  • edited September 2017

    Creating it in a new tab did the trick...

    • Unless that was a ".java" named tab file, merely creating another tab (which is ".pde" by default) wouldn't suffice! :-@
    • ".java" tab files allows us to define classes & interfaces as "top" indeed. $-)
    • However, ".java" tab files can't directly use Processing's API and its sketch's canvas w/o its reference. :-SS
    • Simplest workaround is defining your interface outside its inner class. :-\"
  • @GoToLoop - always good input, thanks. One follow-up question: how do we define an interface outside of an inner class? Right now I have an interface in its own tab, and according to my understanding by default this means it is already in an inner class. Thanks in advance - Andy

  • edited February 2018

    How do we define an interface outside of an inner class?

    • @andytilia, just don't define them inside anything. ;;)
    • BtW, we can't have interfaces inside inner classes anyways. #-o
    • Only interfaces, top-level classes or nested static classes can have interfaces as their members. :-B

    Right now I have an interface in its own tab; and according to my understanding, by default this means it is already in an inner class.

    • An interface isn't a class. So it can't be a class by definition. :-\"
    • Also, interfaces are never inner, b/c they're implicitly static. >-)
    • At most, they can be nested interfaces if they're defined inside some class or another interface. :>
    • Now, if that tab is of extension ".pde", your interface can only be nested. L-)
    • Otherwise, if it's a ".java" tab, your interface is much probably a top-level 1. ~O)
Sign In or Register to comment.