aspose file tools*
The moose likes Performance and the fly likes Implemetinng the interface with all static final variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Implemetinng the interface with all static final variables" Watch "Implemetinng the interface with all static final variables" New topic
Author

Implemetinng the interface with all static final variables

Baiju Varugese
Ranch Hand

Joined: Dec 16, 2003
Posts: 74
hi all,

in my project we have an interface application constants which has all the application level constants defined there and most all the other classes implements ApplicationConstants interface,

i have a doubt that if we implements the interface is there is a seperate copy of all the variables in the heap


Measure twice cut once carpenter theory <br /> <br />Baiju <br />SCJCP, SCBCD, IBM-486, IBM-484, SCEA Part I
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I have no doubt that you are using the "Constant Interface Antipattern"

In any case, if you define "static final int MAX = 10", the compiler is likely to fold that 10 into code. Try the following: compile and run this code. Change the value of MAX and compile only I, then run again.

As far as worrying about separate copyies of constants, why worry? I doubt if that could be a space issue.


There is no emoticon for what I am feeling!
Baiju Varugese
Ranch Hand

Joined: Dec 16, 2003
Posts: 74
hi
thank you,

my consern with the space is my interface consists more than 100 variable and my most of the application classes implements this interface, so i think it as a poor design instead of that access directly Appinterface.var where ever it is required
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I think the results of inheriting the anti-pattern constants interfaces versus using the interface directly will be the same. Inheriting itself doesn't imply copying. I think what whereever you *use* a constant, there may be constant folding, but that doesn't take up space and in fact may save in both space and time as it facilittates further optimization.

On the other hand, good OOD would suggest that if these constants are global and not associated with any one class you should define them in a class:

If you don't like writing AppConstants.MAX elsewhere, you can use static imports.

Client code still will probably fold in constants at compile time. You can test that with the following program: run C, edit and compile only AppConstants and run C again.

To avoid this behavior (to make it easier to recompile and run, versus any worries of runtime size), you can write the constant class as follows.
Pratheep Nair
Greenhorn

Joined: Sep 20, 2005
Posts: 14
If your class implements the constants interface, it becomes part of your concrete class's public API, which is not a good practice. Implementation details, such as using the static members of another class, should not leak into public APIs. As someone mentioned, if you are on 5.0, you can use static imports.
Baiju Varugese
Ranch Hand

Joined: Dec 16, 2003
Posts: 74
Hi,

Thanks for All.

I think static imports works for me.
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
String is immutable,then why do we need to make it a public static final?
Is there any performance advantage or simply a best practice?
Baiju Varugese
Ranch Hand

Joined: Dec 16, 2003
Posts: 74
string is immmutable if you change the value of the string then it will create a new string reference to avoid this
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Originally posted by thomas davis:
String is immutable,then why do we need to make it a public static final?
The "final" modifier applied to a variable means the value of the variable can't be changed. This has nothing to do with whether the variable contains a reference to a mutable or immutable object. Example:is legal because name isn't final. No string objects are changed, they are immutable anyway. Butis not legal because name is final.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implemetinng the interface with all static final variables