wood burning stoves 2.0*
The moose likes Java in General and the fly likes Need help!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Need help!!" Watch "Need help!!" New topic
Author

Need help!!

Gaurav Saxena
Greenhorn

Joined: May 28, 2002
Posts: 29
Hi all
Sorry if i've posted this in wrong place(I beleive it shud be posted in advance java ) anyways lets continue....
I know it's a bad practice - to declare Constants in an interface and then implement that interface in the classes using those constants. But thats a different topic for discussion. I've come across a peculiar situation, a framework to be precise which is as follows :-

1. There is a Base Interface Say A - This interface has "n" no. of String Constants used in the application and absolutely no methods at all. Sample code is as follows
public interface A{
String AAA = "abc";
String BBB = "def";
String CCC = "ghi";
....
....
}

2. There are two more interfaces B and C. Both B and C extend interface A and both have different sets of methods.

public interface B extends A{
String method A() throws SomeException;
void method B(ParamA a, ParamB b) throws SomeException;
String method C(ParamC c) throws SomeException;
}

public interface C extends B{
String method D(ParamE e, ParamF f) throws SomeException;
}

The above three interfaces are a part of the basic framework which the application uses for customization according to different situations.

3. now there is a Class say Class X which is required to implement both interfaces as it has to implement both sets of methods.

Class X implements B,C{
.........
.........
}

My question is - "How will the Java Compiler treat all the String variables declared in Interface A, when we compile the Class X?
As Both interfaces B and C extend A, and X implemets both B and C,will the Class X actually be getting each Variable of interface A twice - once thru the inheritance tree A-->B-->X and second time thru the inheritance tree
A-->C-->X"
All i can say is it's bad and Confusing design according to me...
Can some one throw some light on what happens behind the scenes when class X is compiled?
Thanx and Regards
Gaurav


Gaurav Saxena<br />Programmer Analyst<br />Portland, Oregon
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No copies are made of the constants (other than copies that are inlined into code.) When a class implements an interface, the compiler doesn't copy the interface's constants into that class; there's just one set of static values associated with the interface itself.


[Jess in Action][AskingGoodQuestions]
Gaurav Saxena
Greenhorn

Joined: May 28, 2002
Posts: 29
Ok Ernest as you say
---------------------------------------------------------------------------No copies are made of the constants (other than copies that are inlined into code.) When a class implements an interface, the compiler doesn't copy the interface's constants into that class; there's just one set of static values associated with the interface itself.
---------------------------------------------------------------------------
then why is it discouraged to have static Constants in an interface and then let all the required files implement that interface? I believe one of the reasons is that if you modify the value of constants in the interface then all the classes implementing the interface will have to be recompiled in order to access the modified constants. But on the other hand if u place all the static constants in a normal java class then modification of the constants' value will not require the classes using this class to be recompiled. Is that true? Will be of gr8 help if u can throw some more light on what exactly goes on behind the scenes when a class extends another class or implements an interface.
regards
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
because an interface defines a contract and a constant isn't a contract but an implementation detail.

Therefore constants don't belong in interfaces, they belong in implementation structures be they classes on configuration files/databases.


42
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Gaurav Saxena:
why is it discouraged to have static Constants in an interface and then let all the required files implement that interface?


Because it contaminates the public interface of the class: every client of the class can see the constants, though they are only used internally.

With Java 5, you'd still put the constants in an interface, but statically import them, instead of inheriting them.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help!!