• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Static methods Vs default methods in implementing class

 
Greenhorn
Posts: 4
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this compile? Am I not trying to override a static method here ?



As this does not compile.

 
Saloon Keeper
Posts: 10307
217
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is because of the history of the Java language.

Static methods in interfaces were only added in Java 8. By that time, many classes were written that implement existing interfaces. When the developers allowed for static methods to be added to interfaces, they wanted to make sure that existing classes didn't suddenly break because the interface that they implemented had added a static method that happens to have the same name as a method in the class.

If interfaces had supported static methods from the very beginning, the first snippet of code probably would have caused a compilation error as well.
 
Nation Chirara
Greenhorn
Posts: 4
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The reason is because of the history of the Java language.

Static methods in interfaces were only added in Java 8. By that time, many classes were written that implement existing interfaces. When the developers allowed for static methods to be added to interfaces, they wanted to make sure that existing classes didn't suddenly break because the interface that they implemented had added a static method that happens to have the same name as a method in the class.

If interfaces had supported static methods from the very beginning, the first snippet of code probably would have caused a compilation error as well.



This drove the point home, I got you thanks!
 
Saloon Keeper
Posts: 3291
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one little snag, though.

Normally we say that you must program to the interface, like: List<...> list = new ArrayList<>();
But if you do that here:

then b.m1() is illegal.
 
Bartender
Posts: 2219
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:then b.m1() is illegal.


If you program to the interface, you won't need an instance method called m1() as such method is not defined in the interface.

Similarly, when you program to interfaces with lists like List<String> list = new ArrayList<>(); you don't expect to be able to call list.ensureCapacity(123);.
 
Stephan van Hulst
Saloon Keeper
Posts: 10307
217
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on what Piet said, "Programming to the interface" is advice that is only applicable to calling instance methods.

Static methods should only be called by qualifying the call with the declaring type name (e.g. calling B.m1() ).

Sadly, with static methods declared in classes, you can call them through an object reference (b.m1() ). With static methods in interfaces, the Java designers wizened up and made it illegal to call them through object references.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!