This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum vs Constant File

 
srinivas srinivasmeenavalli
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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 ?
 
Stephan van Hulst
Bartender
Pie
Posts: 5893
63
  • Mark post as helpful
  • send pies
  • 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.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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.
 
Arun Chidam
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic