4
public interface B { String iname = "TBA"; int iid = 0; public static void main(String[] args) { System.out.println("Hello"); } public static void goForIt() { System.out.println("Went for it"); } public void doSomething(); } 

And now...

public class D { public static void main(String[] args) { B.goForIt(); } } 

This successfully printed "Went for it". I was told that interfaces cannot have static methods though. So, what's going on here? However, when I have a class implement B, then the static method doesn't work.

6
  • 2
    Try compiling on Java 7. Commented Mar 9, 2015 at 20:11
  • Hmmm...I think I'm using the latest JDK 8. Commented Mar 9, 2015 at 20:14
  • 2
    This was added in Java 8. Commented Mar 9, 2015 at 20:14
  • 2
    It changes a whole host of things. In fact, it changes so many things that reading literature on anything pre Java 8 is probably not a good idea. Commented Mar 9, 2015 at 20:16
  • 1
    @BoristheSpider Yes. Design simple imperative language with garbage collector -> get mindshare based on easy learning curve -> add generics -> add lambdas -> now a functional programming language with mindshare Commented Mar 9, 2015 at 20:22

1 Answer 1

3

Java 8 has introduced the ability to have static methods in interfaces.

Enhancements in Java SE 8

... In addition, you can define static methods in interfaces.

But static methods are still not inherited, either from implementing an interface or from a superclass.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.