Skip to content

Instantly share code, notes, and snippets.

@alexandreaquiles
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandreaquiles/10394860 to your computer and use it in GitHub Desktop.
Save alexandreaquiles/10394860 to your computer and use it in GitHub Desktop.
Métodos estáticos de interfaces só podem ser acessados diretamente pela interface.
interface Interface {
static void i(){
System.out.println("interface");
}
}
class ClasseMae {
static void a(){
System.out.println("mãe");
}
}
class ClasseFilha extends ClasseMae implements Interface {
static void b(){
System.out.println("filha");
}
}
public class Main {
public static void main(String[] args) {
ClasseMae a = new ClasseFilha();
a.a(); //dá warning no Eclipse: "The static method a() from the type ClasseMae should be accessed in a static way"
a.b(); //obviamente, dá erro
//no Eclipse: "The method b() is undefined for the type ClasseMae"
//no javac: "error: cannot find symbol / symbol: method b() / location: variable a of type ClasseMae"
Interface i = new ClasseFilha();
i.i(); //estranhamente, dá erro.
//no Ecipse: This static method of interface Interface can only be accessed as Interface.i
//no javac: "error: illegal static interface method call / the receiver expression should be replaced with the type qualifier 'Interface'"
}
}
@asouza
Copy link

asouza commented Apr 10, 2014

Achei interessante dar esse erro :). Para min deveria dar erro de compilação para qualquer acesso de método de estático como se fosse método de instância :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment