tags:

views:

235

answers:

7

For example:

public void doSomething() {

    final double MIN_INTEREST = 0.0;

    // ...  
}

Personally, I would rather see these substitution constants declared statically at the class level. I suppose I'm looking for an "industry viewpoint" on the matter.

+9  A: 

I would think that you should only put them at the class level if they are used by multiple methods. If it is only used in that method then that looks fine to me.

chills42
Thanks chills. I was thinking in that direction, but was curious to hear if there were any reasons to avoid the method declaration. For example, I find too many contstants declared in the method to be distracting from the logic.
Liggy
I totally agree, I think it's important to keep the constant value as close as possible to where it is being used, so over time you naturally end up reviewing the constants to make sure they still make sense. As noted, if more than one method uses it then the constant should be class level.
Kendall Helmstetter Gelner
If the constant contains a piece of information that the user would like to get their hands on (default values), then it should be scoped at the class level. In my answer below, I showed that MIN_INTEREST might need to be accessible via the public api, and therefore, should be scoped at the class.
chris
+1  A: 

The reason why you can define a final variable at a class level or method (local) level it's because you can override the global static constant inside the (local) method.

Example:

public class Test {

    final double MIN_INTEREST = 0.0;

    /**
     * @param args
     */
    public static void main(String[] args) {


        Test test = new Test();

        test.doSomethingLocal();
        test.doSomethingGlobal();

    }

    public void doSomethingGlobal() {

        System.out.println("Global-> " + MIN_INTEREST);

    }

    public void doSomethingLocal() {

        final double MIN_INTEREST = 0.1;

        System.out.println("Local-> " + MIN_INTEREST);

    }
}

The output will be:

Local-> 0.1
Global-> 0.0

So your question doesn't make any sense.

sakana
Sorry if the question is a bit unclear.I'll rephrase the question. Are method scoped substition constants common practice? I think chills42 provided a fairly sound answer.
Liggy
A: 

you should also think about thread-safety. declaring some vars on class level could be potentially very thread-UNsafe.

just something to keep in mind

nkr1pt
You are right about variables - however, the question deals with constants. Classlevel-constants are perfectly threadsafe.
Mo
A: 

hi there,

I've used this method scoped constants myself BUT every so often a colleague will down mod it during code reviews. Again, these colleagues are NOT into reading/writing open source BUT they are used to enterprise software.

I tell them that it does NOT make sense to use a class level constant if it is used within a single method BUT I've found more than 1 colleague insisting that it be moved UP. I usually comply since I'm NOT so rigid unless it affects readability and/or performance.

BR,
~A

anjanb
+3  A: 

My starting position is that every variable or constant should be declared/initialized as close to it's first use as possible/practical (i.e. don't break a logical block of code in half, just to declare a few lines closer), and scoped as tightly as possible. -- Unless you can give me a damn good reason why it should be different.

For example, a method scoped final won't be visible in the public API. Sometimes this bit of information could be quit useful to the users of your class, and should be moved up.

In the example you gave in the question, I would say that MIN_INTEREST is probably one of those pieces of information that a user would like to get their hands on, and it should be scoped to the class, not the method. (Although, there is no context to the example code, and my assumption could be completely wrong.)

chris
+1  A: 

Information hiding and modularity are key principles, and narrow scoping is better information hiding. If the constant is only needed by the method, the hiding is good. If and when the constant is useful elsewhere, bring it out to a broader scope, but only as broadly as needed.

You are probably concerned because this is a constant, and therefore, it may seem to belong to some global properties table. Maybe it does. Maybe it doesn't. Your concern is valid, but there is no one best place for all constants.

dongilmore
+1  A: 

Technically, there is no such thing as a "method scoped constant" in Java. What you are referring to is simply a final local variable; it is created an destroyed with each method invocation.

http://www.java-tips.org/java-se-tips/java.lang/how-do-i-declare-a-constant-in-java.html