Lew <> wrote in news:htclj0$7p5$:
> Rhino wrote:
>> Specifically, I have about 20 classes right now in the main part of
>> the project that I'll be putting in the portfolio. There are a
>> substantial number of constants used in one place or another. I have
>> _most_ of them in a single class that is called FooConstants (where
>> "Foo" is replaced by the name of my project.) FooConstants has a
>> private constructor that throws UnsupportedOperationException (as
>> suggested previously in this group) and everything else in it is a
>> static final, i.e. a constant.
>
> It's silly to throw that exception in the private constructor. Only
> the class itself can invoke the constructor. I prefer a private
> constructor with an empty body.
>
Someone on this group recommended that approach to me a few years back. I
don't remember who and I probably wouldn't say if I did; everyone's
entitled to be wrong once in a while
I always wondered why that exception was supposed to be useful....
> "static final" is not the same as a "constant variable" in Java. A
> constant can be non-static, and a final can be non-constant.
>
Sorry, once again I'm being imprecise. Blame it on the fact that my
knowledge of the terminology is less than complete....
The reason this question is coming up is probably that I slavishly
followed advice I was given at some point in the past and never really
understood all the reasons for that advice - or why it might not be the
right advice.
Originally, I seem to recall putting constants in interfaces, then being
told it was bad - I forget why - and that a standalone class for
constants alone (the ones that public static final and where you write
the names in caps with underscores for separators) was the way to go (as
well as the private constructor).
Obviously, I have to rethink this now too.
Man, it seems like I'm having to rethink just about everything I've
written. Working alone without a live mentor or regular walkthroughs with
people who know more than you can definitely put you into a bad place....
Maybe I need a different job strategy: instead of trying to convince
employers/clients that I know what I'm doing, appeal to them for a job on
the grounds that I want to learn how to do things RIGHT, instead of
floundering like this.....
>> Is there any reason NOT to put _all_ of the constants used throughout
>> the project in FooConstants? Or is it wiser to only move something to
>> a XXXConstants class if it is used in more than one class?
>
> Depends. Probably not wise to put them all in one place - some
> constants (or static finals) should be private, not public, ergo kept
> in the class where they're used. Tightly coupling classes together is
> a bad idea often.
>
Okay....
> Like everything else, you have to do what makes sense. Some constants
> should be in a project-wide class, sure. Others in a functional unit,
> for example, data-access constants in a data-access utility class.
> Others should be package-private. Some should be protected or
> private, for use only in an inheritance hierarchy or the class itself,
> respectively.
>
> You have to think each choice through. Access should be as wide as
> needed but not wider, and as narrow as possible but not narrower.
>
Okay....
Darn, I was hoping that at least one thing in Java would be a simple
obvious thing that I wouldn't have to think about for a long time.
I LOVE what this language can do but it's discouraging to find that every
tiny thing needs to be thought of very carefully to keep from doing
something that will have bad consequences. COBOL, REXX and CSP were
definitely easier in that regard

But so much more limited too!
>> For instance, if I have a constant named FOO_FILE_NAME and it is used
>> only in class FooBlahBlah, is it better to leave the constant in
>> FooBlahBlah rather than forcing developers to have to go to another
>> class to determine (or change) its value? Or is it better to have all
>> constants, wherever used, all together in FooConstants?
>
> I usually put static finals generally, and constants particularly, as
> private in the class where they're needed, and give them wider access
> only when needed as needed. YAGNI.
>
That's the second time I've seen YAGNI in the last few days but I still
don't know what it means. I'm guessing it's something like YMMV, Your
Mileage May Vary?
>> Otherwise, if I put a
>> unique occurrence of each multiply-occuring constant, like
>> FOO_LOG_PATH, in each class where it occurs, it seems to be almost
>> inevitable that FOO_LOG_PATH will eventually have different values in
>> each of the classes where it is used.
>
> Logging is usually configured in an external resource, like the
> log4j.properties file for the log4j framework.
>
Right. Like Arne said, things like that should be in a properties file,
coded as constants. Now I just have to get clearer on what should and
shouldn't be put in properties files. I may not have a lot of constants
in a centralized class by the time I'm done this exercise
Thanks for your guidance, Lew!
--
Rhino
---
news://freenews.netfront.net/ - complaints:
---