Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help!!

 
Gaurav Saxena
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaurav Saxena
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic