• 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

Implemetinng the interface with all static final variables

 
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 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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Baiju Varugese
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
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for All.

I think static imports works for me.
 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
string is immmutable if you change the value of the string then it will create a new string reference to avoid this
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic