aspose file tools*
The moose likes Beginning Java and the fly likes why fields are constant in Interfces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why fields are constant in Interfces?" Watch "why fields are constant in Interfces?" New topic
Author

why fields are constant in Interfces?

Bk Jacky
Ranch Hand

Joined: Jun 11, 2005
Posts: 74
Hi,

Why fields are static and final in interfaces?Why interface architecture like so?


SCJP1.4<br />SCWCD1.5<br />"Nothing is impossible"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
npk chowdary
Greenhorn

Joined: Dec 09, 2007
Posts: 21
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
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
shibaram sahoo
Greenhorn

Joined: Sep 21, 2005
Posts: 17
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 ?


We can do it !!
http://letitsearch.googlepages.com/a.html
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31076
    
233

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
[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
Marshal

Joined: May 26, 2003
Posts: 31076
    
233

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why fields are constant in Interfces?