This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes doubt in interface data members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in interface data members" Watch "doubt in interface data members" New topic
Author

doubt in interface data members

Abhi murali
Greenhorn

Joined: Jan 28, 2010
Posts: 12
    
    3
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

Joined: Oct 01, 2001
Posts: 2853
    
  11

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

Joined: Jul 11, 2010
Posts: 247

Since Interface provides a facility of having abstract methods... And instances cannot be created for Interfaces... So it is declared by default as static


SCJP 6 [SCJP - Old is Gold]
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2853
    
  11

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

Joined: Jan 28, 2010
Posts: 12
    
    3
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

Joined: Apr 26, 2011
Posts: 4

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.


Thanks & Regards -
Sudipta
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
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.
 
GeeCON Prague 2014
 
subject: doubt in interface data members