33

Why are interfaces allowed to have a main method in Java 8?

As stated in below code it works fine and produces output properly.

public interface Temp {
    public static void main(String args[]){
         System.out.println("Hello");
    }
}

Currently it is behaving like a class and I have executed interface with main method.

Why do we need this?

4
  • 11
    I think you have the question backwards. The question is not "should main methods be allowed", it is "should we have explicitly disallowed main methods now that static methods are permissible in interfaces" (or, equivalently, exclude interface classes as targets for the java launcher.) This would have been adding extra rules just to exclude something some people perceive as weird (but not dangerous) -- and that's a losing game. You want to define as few new rules as you can reasonably get away with -- because otherwise you get mired in complexity. Oct 23, 2014 at 13:41
  • @BrianGoetz Yes agree!! I am looking for the same thing as I have expressed it with output and my concern is, why do we have an interface which is actually behaving like a class and Java is able to execute the main method of interface.At the end interface is actually a class and java can not differentiate between main of interface and class.
    – akash
    Oct 24, 2014 at 2:15
  • 6
    Interfaces are class types, and always have been. They have some different defaults (methods are public and abstract), and some different restrictions. In Java 8 we removed some restrictions -- such as the restriction against static methods. We're not going to make a special rule to outlaw one particular static method just because it seems weird at first to a few people. When you really understand the new rules, this won't seem weird -- in fact the restriction you propose will seem weird. Oct 24, 2014 at 14:03
  • @BrianGoetz but inside interface method should be abstract. does this rule also get removed in Java8 in case of static method? Dec 8, 2015 at 12:07

5 Answers 5

38

Since Java 8, static methods are allowed in interfaces.

main() is a static method.

Hence, main() is allowed in interfaces.

We don't need this, since it wasn't allowed before, and yet we survived. But since static methods, by definition, are not bound to an instance of a class, but to the class itself, it makes sense to allow them in interfaces. It allows defining utility methods related to an interface (like the ones found in Collections, for example), in the interface itself, rather than a separate class).

There is no difference between class static methods and interface static methods.

4
  • 6
    There is at least one small difference between static methods on classes and interfaces: static methods on classes can be "inherited" by subclasses but static methods on interfaces are not inherited by any subtypes. Oct 25, 2014 at 20:32
  • 4
    @StuartMarks, the inheritance of static methods is only a compiler magic as declaring the same method with the same signature will shadow the static method in the super class. In byte code the static methods are just invoked via fully qualified invocation on class+method.
    – bestsss
    Oct 29, 2014 at 8:30
  • 1
    @bestsss: this is not correct. static methods are inherited and the compiler will use the formal target type found in source code, to be resolved (again) at runtime. The invocation contains a reference to a class+method, but that applies to virtual method invocations as well. The key point is that the actual method declaration doesn’t have to be in the specified class, but could be in a superclass instead. See ideone.com/n9Caa2 (and that’s fundamentally different to static methods in interfaces)…
    – Holger
    Dec 5, 2017 at 8:54
  • 1
    @Holger inheritable, but not overridable, pretty weird situation
    – Eugene
    Dec 5, 2017 at 11:43
14

I second the answer of @jb-nizet. There is no "desparate need" for this, but it removes an unnecessary restriction. E.g. one example is, that you can now declare a factory method within the interface:

 public interface SomeService {

   public static SomeService getInstance() {
     // e.g. resolve via service provider interface
   }

   ...

 }

Before Java 8 we needed always a separate factory class. One favorite example is the google app engine API.

1
  • 4
    So a plausible use of a main method on an interface would be to call the factiry to get an object, then delegate to that object to perform the operation of the program.
    – Raedwald
    Oct 26, 2014 at 10:08
7

In Java 8 an interface can have static methods. Since the main method is also a static method, it will allow it.

We can declare common helper methods using these static methods.

2

From Brian Goetz's in the comments:

I think you have the question backwards. The question is not "should main methods be allowed", it is "should we have explicitly disallowed main methods now that static methods are permissible in interfaces" (or, equivalently, exclude interface classes as targets for the java launcher.) This would have been adding extra rules just to exclude something some people perceive as weird (but not dangerous) -- and that's a losing game. You want to define as few new rules as you can reasonably get away with -- because otherwise you get mired in complexity.

1

More of an addendum: maybe one thought here is to resemble what you can do with the Application trait in Scala:

object Main extends Application {
  Console.println("Hello World!")
}

simply by extending Application, you turn a class into something that runs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.