• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How to define constants for several classes?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
enum
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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

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

with keys,
--------
property file.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic