Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constants

 
Venkatesh Manohar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to store constants and use them in two different classes where can i store it. i have to pass constant names as parameters to methods in them.
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to be rude, but I don't understand what you are trying to say, and you did not even ask a question.
I guess you're asking how to declare a constant that you can pass between two different classes. In that case, you should learn about static variables. With a static variable, it is shared between all instances of one class. For example:

if you make let's say five instances of class A, they all share num. Take one of those classes and add... how about 9.

Well num is updated to 9. You could take any instance of that class now, and when you call getNum() it will return 9.
I hope I've been helpful.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I understand is that you want to have a common class containing constants which you could use across several classes.

I propose the following approach

public class Constants
{
public static final String s="";
.
.
}

Then you can use these constants in any class you wish, and you wont have to worry about its value being changed, as we have made them final. And in case you need to change the value of any variable, it needs to be done from a single place.

Njoy!!
Sid
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes and interfaces whose sole purpose is specifying constants are a sign (not an absolutely sure one, though) of bad OO design. It's like you decided to write a bit of old-style COBOL!

In general, constants should be declared in a proper instantiable class whose purpose most closely matches the meaning of the constants. If other classes need to use those constants, the constants should be declared public (or package-private, maybe).

I might make an exception for very global constants, like fundamental physical constants, common unit conversions etc.
[ April 17, 2007: Message edited by: Peter Chase ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venkatesh,

Go with enum!!!
Best choice as I think for your need (if I understand it well).





Regards,
cmbhatt
[ April 17, 2007: Message edited by: Chandra Bhatt ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you don't advocate enums for all constants? That would be hideous!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic