jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes interface having constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "interface having constants" Watch "interface having constants" New topic
Author

interface having constants

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi Folks

Please can you give me a small demo in form a program I want to know that an interface can have constants that are static and final but then how can a class implementing that interface can use those constants while implementing that interface p, please provide a small example..!! Thanks

there is one url is for reference ..webpage
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

Like any static field, the constants are accessed with the class/interface name.variable name. The general principle being that when you're using the code (if you didn't have the javadoc or source) you can't tell the difference between a constant declared in an interface and a class.



Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202



Hi ,

Thanks for the explanation could you please explain once again in detail
(if you didn't have the javadoc or source) you can't tell the difference between a constant declared in an interface and a class.
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi ,

Why do we keep all the constants in an interface ...and why not in abstract class..what so special of keeping the constants in an interface itself...is there any design principle behind it..?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Saral Saxena wrote:Hi ,

Why do we keep all the constants in an interface ...and why not in abstract class..what so special of keeping the constants in an interface itself...is there any design principle behind it..?


Personally, I always thought it seemed like a bad idea, and it led to the awful constants-only-interface antipattern. I don't think there's any design principle behind it. I think it's just for convenience and brevity. I've been working with Java for many years, and I don't think I've ever done it, or ever would.
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

Thanks for the explanation could you please explain once again in detail

(if you didn't have the javadoc or source) you can't tell the difference between a constant declared in an interface and a class.

Just saying that you access the constants in the Car interface the same way you'd access them if Car was a class. Both ways it's still Car.NUM_WHEELS.

I don't do constants in an interface normally either. It seems to me a bad idea to put implementation details such as data into a class that describes a behavior contract. In the example, what if I wanted to make a ThreeWheeledCar? It would still implement the behavior, drive() but the constants would get in the way. At least if you do it in an abstract class you have the option to use the abstract class when doing the implementation, or you could implement the interface directly and create your own constants.
 
jQuery in Action, 2nd edition
 
subject: interface having constants
 
Similar Threads
constants (only) in interfaces
Usage of Constants on interfaces
Need help!!
Question for Bear Bibeault about ccc.jar
Why they keep all variables in a interface as---> public "static" "final"