Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in interface data members

 
Abhi murali
Greenhorn
Posts: 12
3
  • Mark post as helpful
  • send pies
  • 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.
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Sudipta Banerjee
Greenhorn
Posts: 4
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic