wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constants" Watch "Constants" New topic
Author

Constants

Venkatesh Manohar
Greenhorn

Joined: Dec 27, 2006
Posts: 12
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

Joined: Jul 11, 2006
Posts: 143
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.


Studying for SCJP 6
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
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

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Surely you don't advocate enums for all constants? That would be hideous!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constants