• 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

Enum vs Constant File

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

maintaining all constants in a constants java class is one of general practices of java coding.
Q) Why can't we add constants to an Interface ? We can access constants by importing an interface in source code

Q ) I can achive the same through Java Enum also instead of a constants java class . Which gives better performance ?
What do you suggest , why ?
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't use an interface because this isn't what interfaces are meant to do. Interfaces tell the world what a class can do. They are not a bucket for values.

Enums do exactly that. They enumerate a list of constants. Conceptually enums are superior by far for this purpose.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srinivas srinivasmeenavalli wrote:Hi,

maintaining all constants in a constants java class is one of general practices of java coding.



Not my practice and not a practice mandated or even encouraged anywhere I have worked.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some time it is hard to debug if you use constant interface . let say you have an constant interface called Constant and it contains some constant(say VALUE="javaranch") which is used by file called Main.java. now, you decided to change the value to "coderanch" and compiled the Constant.java but the value wont be reflected in Main.java unless you recompile Main.java again. because of the VALUE is compile time constant. If you use enum then recompilation is not required;enum is a runtime constant.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:let say you have an constant interface called Constant and it contains some constant(say VALUE="javaranch") which is used by file called Main.java. now, you decided to change the value to "coderanch" and compiled the Constant.java but the value wont be reflected in Main.java unless you recompile Main.java again. because of the VALUE is compile time constant. If you use enum then recompilation is not required;enum is a runtime constant.



Why we need to compile Main.java when we changed only the value of constant in Constant.java?, is i'm missing something here
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Chidam wrote:Why we need to compile Main.java when we changed only the value of constant in Constant.java?, is i'm missing something here


compiler always makes a own copy of compile time constant to Main.java.
for example, in Main.java
if you assign var = Constant.VALUE ; then at compile time compiler replace Constant.VALUE with actual value. i.e, var = "javaranch";
 
srinivas srinivasmeenavalli
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Not my practice and not a practice mandated or even encouraged anywhere I have worked.


James Sabre , what is your practice ? Did you use Properties files to have constant values, which never be changed ?
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srinivas srinivasmeenavalli wrote:


Not my practice and not a practice mandated or even encouraged anywhere I have worked.


James Sabre , what is your practice ? Did you use Properties files to have constant values, which never be changed ?



I put the 'constants' in the smallest scope they are needed in. A set of state machine constants that are needed only with a small number of classes within a single module which is part of a larger program will be declared in a class within that module and will be private to that module. Constants that are associated with a module and part of the public interface of that module will be placed in a class associated with that module and it will contain only constants that are part of that module public interface and no others. Constants used throughout a program will be placed in a class or properties file that is accessible to the whole program; this is rare but it does happen.

 
reply
    Bookmark Topic Watch Topic
  • New Topic