text stringlengths 1 330k |
|---|
public final class MyValues { |
public static final String VALUE1 = "foo"; |
public static final String VALUE2 = "bar"; |
in another class : |
import static MyValues.* |
share|improve this answer |
Where's the benefit of creating a separate class here? While I normally don't hold the Calendar API up as a good example of design, it's fine in terms of "calendar-related constants are in the Calendar class". – Jon Skeet Jan 26 '09 at 13:19 |
the benefit is about not duplicating code, in case you need to reuse constants in more than one class. I guess you can easily see the advantage of this. – user54579 Jan 26 '09 at 13:55 |
Why would you duplicate code? Just refer to the other class. I find it relatively rare that a constant really stands alone. – Jon Skeet Jan 26 '09 at 14:50 |
For better readability I'd also have a private default constructor with no body (and a matching comment). – Ran Biron Jan 26 '09 at 19:33 |
Your clarification states: "I'm not going to use enums, I am not enumerating anything, just collecting some constants which are not related to each other in any way." |
If the constants aren't related to each other at all, why do you want to collect them together? Put each constant in the class which it's most closely related to. |
share|improve this answer |
Jon - they are related in the sense they all belong to the same functionality. However, they are not an enumeration of anything... They do not hold the property that an object is "one of those things". – Yuval Adam Jan 26 '09 at 12:34 |
Okay. In that case just put them in the class which is closest to the functionality they're related to. – Jon Skeet Jan 26 '09 at 13:19 |
My suggestions (in decreasing order of preference): |
1) Don't do it. Create the constants in the actual class where they are most relevant. Having a 'bag of constants' class/interface isn't really following OO best practices. |
I, and everyone else, ignore #1 from time to time. If you're going to do that then: |
2) final class with private constructor This will at least prevent anyone from abusing your 'bag of constants' by extending/implementing it to get easy access to the constants. (I know you said you wouldn't do this -- but that doesn't mean someone coming along after you won't) |
3) interface This will work, but not my preference giving the possible abuse mention in #2. |
In general, just because these are constants doesn't mean you should apply normal oo principles to them. If no one but on class cares about a constant - it should be private and in that class. If only tests care about a constant - it should be in a test class, not production code. If a constant is defined in multiple p... |
share|improve this answer |
My preferred method is not to do that at all. The age of constants pretty much died when Java 5 introduced typesafe enums. And even before then Josh Bloch published a (slightly more wordy) version of that, which worked on Java 1.4 (and earlier). |
Unless you need interoperability with some legacy code there's really no reason to use named String/integer constants anymore. |
share|improve this answer |
Aren't enums best choice for these kinds of stuff? |
share|improve this answer |
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 if all your constants are related (like planet names), put the constant values in classes they are related to (if you have access to them), or use a non instanciable utility class (define a private default constructor). |
class SomeConstants |
// Prevents instanciation of myself and my subclasses |
private SomeConstants() {} |
public final static String TOTO = "toto"; |
public final static Integer TEN = 10; |
Then, as already stated, you can use static imports to use your constants. |
share|improve this answer |
Just use final class. |
If you want to be able to add other values use an abstract class. |
It doesn't make much sense using an interface, an interface is supposed to specify a contract. You just want to declare some constant values. |
share|improve this answer |
Or 4. Put them in the class that contains the logic that uses the constants the most |
... sorry, couldn't resist ;-) |
share|improve this answer |
This is not a good idea. This creates dependencies between classes which should have none. – Yuval Adam Jan 26 '09 at 12:33 |
Why not? I'd say that classes that use the same constants have a dependency anyway... And somehow there must be one class that is the most dependent on these constants. – Simon Groenewolt Jan 26 '09 at 22:36 |
If there was only one class using the constants, this could make sense. – Tom Hawtin - tackline Nov 12 '13 at 23:27 |
enums are fine. IIRC, one item in effective Java (2nd Ed) has enum constants enumerating standard options implementing a [Java keyword] interface for any value. |
My preference is to use a [Java keyword] interface over a final class for constants. You implicitly get the public static final. Some people will argue that an interface allows bad programmers to implement it, but bad programmers are going to write code that sucks no matter what you do. |
Which looks better? |
public final class SomeStuff { |
private SomeStuff() { |
throw new Error(); |
public static final String SOME_CONST = "Some value or another, I don't know."; |
public interface SomeStuff { |
share|improve this answer |
but bad programmers are going to write code that sucks no matter what you do. That's only so true. If you can take steps to mitigate bad programming, or eliminate it altogether, then you have a bit of a responsibility to do that; be as explicit as possible. A bad programmer can't extend a final class. – liltitus27 Nov... |
@liltitus27 To some extent I agree. But do you really want uglified code just to stop one way of poor programmers writing bad code? The code they produce will still be hopeless, but now your code is less readable. – Tom Hawtin - tackline Nov 12 '13 at 23:13 |
Your Answer |
The Big Melt – Global Warming |
Climate change | Environmental challenges | Global warming |
Welcome to, your guide to global warming and climate changes. On these pages you will find extensive information about the challenges we are facing and the impact on the environment the climate changes have. |
Global warming begins when sunlight reaches Earth. The clouds, atmospheric particles, reflective ground surfaces and ocean surface then reflected about 30 percent of it back into space, while the remaining is absorbed by oceans, lands and air. This in turn heats the planet’s surface and atmosphere, making life possible... |
The problem begins when the concentration of greenhouse gases in the atmosphere were artificially raised by humankind at an ever-increasing rate since the past 250 years. As of 2004, over 8 billion tons of carbon dioxide was pumped out per year; natural carbon sinks such as forests and the ocean absorbed some... Read m... |
Bookmark and Share |
Department of |
Learning Goals |
Program Goals: |
Goal 1: Knowledge |
It is the goal of the history department to introduce our majors to history from a variety of periods and regions and to familiarize them with a variety of categories/genres of historical analysis. It is also our goal that each major will ultimately acquire in-depth historical knowledge in the area of his or her specia... |
Goal 2: Skills |
While mastery of course content is crucial, it is the primary objective of the history department to teach students skills, such as the ability to interpret primary sources, evaluate secondary sources, communicate effectively, and write a thesis-driven research paper. |
There is alignment between the History Department’s Goals and University Goals. In particular, the department, like the University, is committed to the goals of “cultural competence,” “international learning experiences,” “integrated learning experiences,” and giving a “privileged place to the liberal arts” in … [the] ... |
Student Learning Outcomes |
Outcome 1: Majors can identify significant causes of a given event or historical process and can situate events, ideas, or cultural practices within a larger historical context. |
Outcome 2: Majors can write thesis-driven papers that are clear, grammatically correct, well documented, well organized, and expressive of complex thought. |
Outcome 3: Majors can make a logical and convincing historical argument that is substantiated by primary sources and situated in existing secondary literature. |
My big, fat, lily-white Second Life |
Would any black resident please stand up? |
Transforming your business with flash storage |
I think I'm every bit as beautiful as I was, although it seems that not everyone would agree. I've been in this skin for over two weeks now, and my experiences have definitely been mixed. My in-game friends have remained friends: I haven't watched people who I know well suddenly backing away from me. But strangers do k... |
The new Destiny |
The class ceiling |
Second Life requires a broadband connection and a fairly potent computer to run properly. If you haven't got at least 1G of system RAM, a 128M graphics card, and a 3M broadband connection, good luck. So you can see right there that SL automatically selects residents from the middle-classes, who can afford at least mode... |
But there's more than money at play here: SL selects people who believe that an up-to-date computer system is something worth having. There are plenty of users who get by happily on clunky old machines that run their favourite applications well enough. |
But to really care about your computer's supposed features, you have to be, first, free to worry about that instead of, say, food and the electric bill, and second, a bit toy-proud in a nerdy way. The lower orders express their toy-pride with power boats, trucks, exotic televisions, and above-ground swimming pools. The... |
A myth that I hear repeated by residents is that SL reflects life, because people create it. People like sex, so there's plenty of sex. People like gambling, so there's gambling. People like music, so there's music. People like art, so there's art. |
I've found this to be quite naive. SL reflects a slice of life: a very white, Protestant, progressive, bourgeois slice. I can't recall if it was in Paul Fussell's Class, or Lisa Birnbach's The Official Preppy Handbook that I encountered the fine observation that it is the upper middle classes who typically play at life... |
4 reasons to outsource your DNS |
More from The Register |
next story |
Fear not! Sick-splashed flip flops to rise again online |
World's most pushy investor sticks boot in over VoIP biz flog |
Read the fine print before you shack up with the vast agency |
Twitter blew $36m on patents to avoid death by lethal injunction |
Ad-free streaming service to milk massive mobile market |
Neil Young touts MP3 player that's no Piece of Crap |
Crazy Horse man saddles up 'Pono' for digital audiophiles |
Zuckerberg in drone fleet scramble for Africa – report |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.