• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constants

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely you don't advocate enums for all constants? That would be hideous!
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic