Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which one is better

 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me should I make a class or Interface for declaring constants

OR



Thanks
Vijay
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are bad, neither are typesafe. Use an enum.
 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul
but what in case if I need String constants and I am using java 1.4 which does not support enum.

Regards,
Vijay
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
Both are bad, neither are typesafe. Use an enum.


Enums aren't applicable to all constants, are they? For instance, would you use enums for a set of diverse mathematical or physical constants? (Note: this really is a question, because I have hardly any experience of using enums).

Where enums are unavailable, I think an interface is a bit nicer. But don't be tempted to declare any class as implementing that interface; that's a well-known anti-pattern.
[ August 07, 2007: Message edited by: Peter Chase ]
 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
I am agree with you.because most of the time we need to make string constant.
Is there any performance,memory issue with these two approaches.

Regards,
Vijay
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Enums aren't applicable to all constants, are they? For instance, would you use enums for a set of diverse mathematical or physical constants?

You are, of course, right Peter. My (overly short) answer is based on the inference that Vijay's example describes an enumerated type, rather than a set of "real world" constants. If they don't describe that situation my preference would also be for a final class with a bunch of public static attributes precisely so it can be extended or implemented.
 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul & Peter
I really appreciate your quick reply could you please clarify that is there any performance,memory issue with these two approaches.

Regards
Vijay
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use static because there's no point in making a copy of these things in every instance of the class. I wouldn't go so far as to call it a performance issue as much as just being tidy and more correct in the semantic meaning.

Use final to make it clear to all readers (and the compiler) that you don't intend for these to ever change.

Using an interface depends. If this class is the primary or only consumer of the constants, I'd be happy to put them there. A "Constants" class for the whole system is not appealing. I'd try to keep them close to where you really use them.

Think about whether THREE should equal "2". I assume that was a quick typo.

Another design that works well:

This is not an exact replacement for your Strings. You could compare your Strings against some user-entered String. But if you want people to call your method with one of several values you provide, this will do it. The type on the method argument tells them what they're dealing with where String does not.
[ August 07, 2007: Message edited by: Stan James ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic