• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

access static methods of an interface

 
Greenhorn
Posts: 15
2
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't I access static methods of an interface using an instance variable.

Example:-


Static members of a regular class can either be accessed in static manner or using instance variable(compiler just warns here, but doesn't stop), what is different about static methods of an interface, that compiler doesn't allow us.

Thanks and Regards


 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Udvigna Prakash wrote:
Static members of a regular class can either be accessed in static manner or using instance variable(compiler just warns here, but doesn't stop), what is different about static methods of an interface, that compiler doesn't allow us.



The static methods of an interface, is a very new feature of Java. And as such, is specified in a different section of the Java Language Specification.

I don't know why the behavior is defined differently, but it looks like it is that way. I will, however, speculate that using an instance to access a static method was probably not a good idea, so, when they defined this section, they probably opted to not allow it.

Henry
 
Udvigna Prakash
Greenhorn
Posts: 15
2
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry,

I guess, it is one of those things in Java, which you just have to remember.

Regards
 
Sheriff
Posts: 15784
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just as well that they put as many sensible restrictions on its use, otherwise programmers will find many un-sensible ways to abuse them. Why would you even want to do such a thing that you've shown? Understand their proper use before you use them. These things are like table saws. If you don't know how to use them or you're not careful in the way you use them, you can easily hurt yourself and others.

See this for more info: https://softwareengineering.stackexchange.com/questions/233053/why-were-default-and-static-methods-added-to-interfaces-in-java-8-when-we-alread
 
Udvigna Prakash
Greenhorn
Posts: 15
2
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu,

I never use an instance variable to access static members, but this came up during my preparation for OCAJP 8, so thought of getting inputs from fellow ranchers. It still bugs me a little that in order to enforce good programming Java is suddenly now changing its own rules. Since they can't break old code by making these new rules uniform across the board, it adds little confusion. Hope these things will be clearer, once I complete Java 8 SE syllabus completely.

Regards
 
    Bookmark Topic Watch Topic
  • New Topic