Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
How to define a member interface in a static context in java?

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

Case A: Interface within a top-level class works perfectly

package multiplei.interfaces.test;

public class InterfaceBetweenClass {

    interface Foo {
        void show();
    }

    class InnerClass implements Foo{
        public void show(){
            System.out.println("Inner Class implements Foo");
        }
    }

    public static void main(String[] args) {
        new InterfaceBetweenClass().new InnerClass().show();
    }

}

Case B: Interface within an interface works good.

public interface Creatable {
    interface Foo{
        void show();
    }}

Case C: I know it sounds stupid that why would anyone define an interface in a static context. But it give me the same error message when i try to define the interface in static context.

package multiplei.interfaces.test;

public class InterfaceBetweenClass {
    public static void main(String[] args) {
        interface Foo {  //Line 5
            void show(); 
        }
    }

}}

But line number 5 gives me the following error message "The member interface Foo can only be defined inside a top-level class or interface or in a static context." Please help me out with this If an interface can be defined in static context then how?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You cannot define interfaces within methods.

I think the scenario the error message is referring to is defining an interface inside an inner class (which can be done, but only if that is a static inner class):

class A{
   static class X{
     interface Y{}
   }
}
about 4 years ago · Santiago Trujillo Denunciar

0

For interface, both nested-interface and member-interface are the same thing (#opposite to member-class). Moreover, member-interface is an interface which is DIRECTLY enclosed by another class or another interface. Therefore, local interface DOES NOT exist.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda