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.