• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in interface data members

 
Greenhorn
Posts: 12
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does an interface have public static and final as the modifiers of the data members? Why not just public and final or just public, so that when a class implements an interface the member inherited(?? am i using the correct word) from the interface is used by the class?
Please let me know why other combinations of public ,static and final , 2 taken at a time are not possible.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, interface method aren't allowed to be static or final, and public doesn't make any difference since all the methods are assumed to be public.

You can also use interfaces to bring in constants though, and you are allowed to use static and final on those fields. I'm not sure they make any difference either though.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Interface provides a facility of having abstract methods... And instances cannot be created for Interfaces... So it is declared by default as static
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static abstract methods don't make sense, but static fields do. You can add the public modifier to interface methods, and any combination of public, static, and final modifiers on interface fields. I did some experimentation and none of the modifiers make any difference.



is the same as:


and



is the same as



Interface methods always act like public methods, and interface fields always act like public static final fields.
 
Abhi murali
Greenhorn
Posts: 12
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question was related to variables not the methods.I am sorry, I think I should have been more specific. The variables in the interface are always public static and final.Removing any of public ,static or final does not make any difference.
Now why did the people who designed Java decided that the variables in an interface have to be public static and final?
 
Greenhorn
Posts: 4
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Data members in an interface are final by default because interfaces have no means of initializing its data members, as it does not have constructors or concrete methods. Which is why they are declared as final(and initialized inline).

Data members in an interface are static by default because interfaces can never be instantiated.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Class Object is the root of the class hierarchy. Every class has Object as a superclass.



Interface do not have any super class and do not come into the class hierarchy of the JAVA.
And Constructor is required for any classes which come into this hierarchy, even it is also required for abstract classes.
Constructor is provided a way to instantiate class' object. And as interface do not have it, it can not be possible to put instance members in it.

Interface methods just provide a contracts for other classes to use it and also forces(by abstract keyword) to give an implementation for it.
And with public keyword, any class can access them.

Interface has only constants(therefore they are final), and not instance variables.
By declaring static, it say that it is (class) constant for every classes which will satisfy its method's contract.
And with public keyword, any class can access them.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic