aspose file tools*
The moose likes Java in General and the fly likes Enum vs Constant File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enum vs Constant File" Watch "Enum vs Constant File" New topic
Author

Enum vs Constant File

srinivas srinivasmeenavalli
Ranch Hand

Joined: Jul 13, 2008
Posts: 65
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: May 08, 2006
Posts: 79
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


SCJP 1.4, SCWCD 1.4
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jul 13, 2008
Posts: 65

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

Joined: Sep 07, 2004
Posts: 781

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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Enum vs Constant File