Are constants related to each other

DISABLE ADBLOCK

ADBlock blocks some content on the website

question

Suppose you need to define a class that will keep constants all of it doing.

What is the preferred way of doing this?

  1. interface
  2. Abstract class
  3. Graduating class

Which should I use and why?


Clarifications on some of the answers:

Enumerations -. I'm not going to use enumerations, I can't am enumerating anything, I can just collect some constants that aren't related in any way

interface - I'm not going to get every class going as one that implements the interface. I just want to use the interface to call constants like this:

solution

a final class. For simplicity, you can then use a static import to reuse your values ​​in another class

in another class:

OTHER TIPS

Your clarification states: "I will not use enumerations, I am not enumerating everything, I can only collect some constants that are not connected in any way."

If the constants are not related at all, why do you want to collect them together? Put each constant in the class that it is most likely to be related to.

My suggestions (falling in order of preference):

1) Do not do it . Create the constants in the current class where they are most important. With a 'bag of constants' class / interface following OO best practices, really not.

I, and everyone else, ignore # 1 from time to time. If this is what you plan to do then:

2) last class with private constructor This will at least prevent someone from abusing your 'bag of constants' by expanding / implementing it to get easy access to the constants. (I know you say you wouldn't do this - but that doesn't mean anyone will come together after you fail)

3) interface This works, but not my preference is the possible abuse mention in # 2.

Usually just because these are constants doesn't mean you shouldn't still apply normal oo principles to them. If no one but on takes care of a constant class - it should be private and in this class. If only tests have constant maintenance - it should be in a test class, not production code. If a constant is defined in several places (and not just the same by chance) - refactor to avoid duplication. And so on -. Treat them like you would a procedure

As Joshua Bloch notes in Effective Java:

  • Interfaces should only be used to define types
  • abstract classes don't prevent instanciability (they can be subclassed, and even suggest that they are designed to be subclassed).

You can use an ENUM when all constants (like planet names) are related, put the constant values ​​in classes they refer to (if you have access to them) or use a non-instanciable utility class (define a private default constructor).

Then, as mentioned earlier, you can use static imports to use your constants.

My preferred method is not to do this at all. The age of constants pretty much died when Java 5 introduced type-safe enumerations. And even before then, Josh Bloch released a (a little more verbose) version of it that worked on Java 1.4 (and earlier).

If you need interoperability with some legacy code there really is no reason to use called string / integer constants anymore.

Just use last grade.

If you want to be able to add other values ​​use an abstract class.

It doesn't make a lot of sense over an interface is an interface is supposed to specify a contract. They just want to explain some constant values.

s are fine. IIRC, an element in Effective Java (2nd Ed), has constants enumerating standard options to implement a [Java keyword] for any value.

My preference is to use a [Java Keyword] over one for constants. You implicitly get the. Some people argue that a bad programmer can implement it, but bad programmers go to write code no matter what sucks you do.

Which looks better

Or:

or 4. Put them in the class that contains the logic that the constants use most

... sorry, couldn't resist; -)

  1. A disadvantage of the private constructor is that procedures in existence could never be tested.

  2. Enum by the nature concept apply well in certain domain type, if they apply decentralized constants doesn't look good enough

The concept of the enum is "Lists are sets of closely related articles".

  1. Extend / implement a constant interface is bad practice, it is hard to think about requesting to extend an immutable constant instead of referring to it directly.

  2. When quality tool like SonarSource applies, there rules force developers are to fall constant interface, this is an embarrassing thing as quite a few projects enjoy the constant interface and seldom "extend" things happen on constant interfaces

Get answers to millions of questions and give back by sharing your knowledge with others.

Sign up for an account.

The contents are licensed under creative commons.

If you find copyright violations, you can contact us at info-generacodice.com to request the removal of the content.