GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes How to define constants for several classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to define constants for several classes?" Watch "How to define constants for several classes?" New topic
Author

How to define constants for several classes?

Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
How do you implement what in C++ would correspond to a header file where constants for use by several classes could be defined? For example, server request codes?

C++ equivalent example:


TIA


-------<br />Thanks & regards,<br />anw
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

enum


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

possible options are:
----------

enum,
constant file ( almost like what you want),

with keys,
--------
property file.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you're using JDK 5 or later, then yes enums are probably your best option. If you're stuck with JDK 1.4 or earlier, you might want to check out this article. Note that it was written before JDK 5 came out, and when it refers to "enum" it means "enum" as used in C/C++, not the newer Java enum.

It's also possible to do something like this:

In general you're better off with either an enum or the typesafe enumeration from the article. But you may find older code that uses public static final ints (or other types), as above.


"I'm not back." - Bill Harding, Twister
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Create a interface with all the constant values in that.This would be the header equivalent in Java and as you import the header in C programs , in java you can implement that interface where you want to use those constants.

Or, you can have a public constant class having all such constant value as static values.Now you can access those using the classname.variablename


Rahul Bhattacharjee
LinkedIn - Blog
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199

Create a interface with all the constant values in that.This would be the header equivalent in Java and as you import the header in C programs , in java you can implement that interface where you want to use those constants.

Or, you can have a public constant class having all such constant value as static values.Now you can access those using the classname.variablename


Using Java 1.5's static-import feature, you can import static members (attributes, methods, classes) of an interface/class.



Using static import enables userto use static members without specifying the class/Interface name.

In above examples context


~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
 
wood burning stoves
 
subject: How to define constants for several classes?