0
public interface Drive {
   int getNumWheels();

   default int driveMiles(){
      return 10;
   }

   static int getColorID(){
     return 0;
   }
}

In the code above, why doesn't the static method in an interface not need a default access modifier? I understand that the default method was added in JDK 8, and provides an optional implementation, however, I am a little unclear about the static method? This seems really similar to a class's static method?

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
eli
  • 335
  • 1
  • 3
  • 18
  • 1
    since you can´t override static methods you wont (and can´t) need to define a default implementation for it, rather give it it´s explicit behavior. – SomeJavaGuy Apr 22 '16 at 07:09
  • But, I don't understand, why did they decide to include static in an interface for JDK 8? what is the benefit? – eli Apr 22 '16 at 07:11
  • even though the [question](http://stackoverflow.com/questions/25934134/static-methods-added-in-interfaces-in-java-1-8) got closed the accepted answer is pretty well in explaining it. A little summary of it is, you wont need to provide utility `classes` for `interfaces` anymore since you can define the functionality of the utility classes in the `interface` now. – SomeJavaGuy Apr 22 '16 at 07:14
  • "default" is not an access modifier, it's a marker for default method implementation. Within an interface only "public" access modifier is allowed to be specified explicitly but it is usualy not needed as it is also implicit access modifier of all interface members. – Lukas Risko Apr 22 '16 at 07:33

2 Answers2

2

In the code above, why doesn't the static method in an interface not need a default access modifier?

I guess you are asking, why is it static and not static default?

In classes you can just use static and most likely they thought it would be simpler to keep it consistent.

This seems really similar to a class's static method?

Which is why I believe the syntax is the same.


A better question would be

why do we use default as a modifier at all?

There is two reasons I can think of

  • default is already a keyword so easy to reuse in the language and it is used in a similar way in annotations.
  • it makes it clear that this method is intended to have an implementation. They could have dropped the need for a keyword at all, but a little redundancy can improve code quality.

why did they decide to include static in an interface for JDK 8?

The benefit is the same as for a class. You can define a method which is associated with an interface but is not tied to a particular class. A good example is factory classes. In the past you would have to have say

Logger LOGGER = LoggerFactory.getLogger(...);

If you wanted Logger to be an interface, it's factory method, which must be static had to use a utility class. Now your API could be

Logger LOGGER = Logger.get(...);

The interface for the factory method is the same as the interface it returns so there is less reason to quality exactly what type get returns.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

There is a difference in purpose of default and static methods.

When you extend an interface that contains a default method, you can do the following:

  1. Not mention the default method at all, which lets your extended interface inherit the default method.
  2. Redeclare the default method, which makes it abstract.

  3. Redefine the default method, which overrides it.

Static Methods

A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

If you are redefining static method in another class, you are hiding the definition in first interface.

Refer to oracle documentation page about default and static methods.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211