aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Constants Class: Good or Bad Idea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Constants Class: Good or Bad Idea" Watch "Constants Class: Good or Bad Idea" New topic
Author

Constants Class: Good or Bad Idea

Devon Richards
Greenhorn

Joined: Aug 22, 2008
Posts: 2
HI,

I am currently working on B&S 2.1.2 and have a question wrt to use of a Constants class to hold the constants used within my classes.
The idea is to have one of these classes per package.

E.g.
final class Constants {
private Constants(){
}

public static final String EXAMPLE = "Example";

}


I will then use this class in my concrete classes for the constants which I need. e.g.

use Constants.EXAMPLE if I require this value in my class.

There is nothing in the SUN Coding convention which prevents me from doing this but I just want some opinions as to whether this is a good idea or a bad one.


Regards,<br />Devon <br /> <br />SCJP 5.0<br />SCDJWS<br />SCBCD 5.0
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

I personally dislike that, I feel that the constant should be declared where they are used, or in whatever object that has the responsibility of knowing this value..

I guess I can see the opposite arguments.. If we remain in the Object Oriented dos and don't, i would say it is bad.

You have to keep the coupling low between classes and this would be very very high.

Regards,
Alex
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
In my project, I don't have something like that. Whenever you can just put the constant in the appropriate class. For example, I have default socket port number and I put it in the class that used by the server to listens for the connection.

hope that helps.

Jeffry Kristianto Yanuar
Java Instructor
SCJP 5.0
SCJA
SCJD (Working on UrlyBird 1.3.2) --> testing and documenting the assignment
Jethro Borsje
Ranch Hand

Joined: Jul 22, 2008
Posts: 100
I think this is a bad idea, because it is not particular Object Oriented.

Consider the following case: you have a constant related to your server, the default port number for sockets for example. If you now decide to remove the entire "SocketServer" because you want an "RMIServer", you would have to look all the constants of the "SocketServer" up in the Constants class. This could easily be forgotten when redesigning your project thereby leaving unused constants in your class.


SCJP, SCJD
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3223
Encapsulate your Enums and Constants in a package or a class where applicable. If you have global constants, put it in a common package or project that all other projects depend on.
[ September 06, 2008: Message edited by: arulk pillai ]

Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Devon Richards
Greenhorn

Joined: Aug 22, 2008
Posts: 2
Thanks for all the feedback, I don't wan't to lose unnecessary marks due to this approach not being OO compliant so I am going to declare and use the constants in the classes were they are going to be used.

Though from a maintainability stand point, as a developer I would rather have constants declared in one class and know that if I am required to change a constant value which could be the same in multiple classes, due to a requirement change, I can change it in one class and not have to search and change the constant in multiple classes.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
If you want a container for constants, an interface will do just as well. I use this quite a lot for handling XML, in a way that I have an interface bearing constants for the element and attribute names specified by a given XML schema. All classes dealing with instance documents of the XML schema now can import the corresponding interface and have the constants for the element and attribute names at hand.
Jethro Borsje
Ranch Hand

Joined: Jul 22, 2008
Posts: 100
Originally posted by Devon Richards:
Though from a maintainability stand point, as a developer I would rather have constants declared in one class and know that if I am required to change a constant value which could be the same in multiple classes, due to a requirement change, I can change it in one class and not have to search and change the constant in multiple classes.

You should still declare a constants which is used by multiple classes only once: in the class where it originates from, or where it is used the most. It is a very bad idea to have the same constant declared in multiple classes.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constants Class: Good or Bad Idea