• 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

why fields are constant in Interfces?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why fields are static and final in interfaces?Why interface architecture like so?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because an interface only describes functionality, not state. A non-constant variable is part of the state of an object (or class, when static). As an interface does not describe the object state, it would be meaningless to have non-constants in it.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you know we cannot create object for an Interface.we can create object for implementors .Think like this, static variables can be accessed by reference ok.so this copy is available for all objects implementing those interfaces.If from one object value changes then other can access that value(change) this will lead to messy,so we place final
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy B Singh,

Welcome to JavaRanch

First thing you got to update is your display name. Ranch is a very friendly forum which has very less strict policies and one among them is the
display name. Please have a look at this page. You can update your name by editing your profile.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have also a similar kind of confusion in my mind..
Please help me out..

Interface is a contract. (I agree)
Interface are means to be used to create generic behaviours (methods) and attributes (variables), so that implementor classes will get through inheritance. (I agree and believe on this)

But my confusion is that, If I my classes gonna get an attribute from an interface, then I can not modify it as per my class requirement.


So why do have all the variables are by default public static final ?
Isn't it would have better like if just public / protected variables (no static)

Then I could have easily get the color through inheritance and whenever I want to change color of my car I could have done that easily..

what do you say..

why do have all the variables are by default public static final, then ?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shibaram,
You could write a getColor() method and have your subclasses implement that. If you want to provide a default implementation, maybe you need an abstract class and not an interface.

Also, "public class MyClass implements MyInterface" is an anti-pattern for inheriting constants. Interfaces are meant to inherit methods or references constants statically. In Java 5+ you can even use static imports to avoid referring to the "constants interface" all over.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Jeanne]:Also, "public class MyClass implements MyInterface" is an anti-pattern for inheriting constants.

Well, not quite. It's an anti-pattern if the constants are only inherited for internal use, because those constants become part of the public API. It's not an anti-pattern if the constants are intended to be part of the public API. The best example I know of in the standard libraries are WindowConstants and SwingConstants. You're supposed to see DISPOSE_IN_CLOSE and such when you look at the public API of a JFrame - you use that constant from outside the class. That's fine. It's when you expose purely internal details that constant interfaces are an anti-pattern.

I admit, I shudder a bit when I hold up something in Swing as an example of good design. And in the modern age (JDK 5+), all the examples I can think of for a valid constant interface pattern would probably be better implemented using enums. But remember these were not available when Swing was designed, and the Typesafe Enumeration pattern was not generally known (if it was known at all). So while I may well have doe other things differently, I believe that the use of constant interfaces in WindowConstants and SwingConstants is perfectly valid.

I suspect this is far from shibaram sahoo's original question. That has already been addressed pretty well - but I would add this: why do you think this sort of functionality should be in an interface, anyway? You could simply create a superclass, perhaps an abstract superclass, to include whatever behaviors you want. Of course you can only extend one class in Java - but if that is your objection, then your question is really: why doesn't Java allow multiple inheritance of implementations? This is another FAQ I think. You could probably find many previous discussions of this issue using the Search of FAQ links provided by the forum. I'll skip that though, since I don't even know if that is really your question.
[ August 29, 2008: Message edited by: Mike Simmons ]
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Simmons:
Well, not quite. It's an anti-pattern if the constants are only inherited for internal use, because those constants become part of the public API. It's not an anti-pattern if the constants are intended to be part of the public API.


That's a good point about the public API.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic