32

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.

1
  • Is there a memory usage advantage to declaring the constant at the class level as opposed to the method level, if the class is a Singleton? Any thoughts: @Chris Cudmore
    – Adam
    Commented Dec 14, 2015 at 18:57

7 Answers 7

45

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.

3
  • 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
    Commented Oct 28, 2008 at 18:12
  • 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. Commented Oct 28, 2008 at 18:49
  • 1
    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. Commented Oct 28, 2008 at 19:27
22

My starting position is that every variable or constant should be declared/initialized as close to its 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 quite 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.)

10

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

1
  • 2
    Broken URL. Is there evidences that it will be re-declared on every method execution?
    – wviana
    Commented Sep 19, 2018 at 12:14
7

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.

1
  • The ailment we [elegant developers] suffer lol Commented Aug 29, 2023 at 9:39
4

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.

3

I have a different take on this: IMHO its better to place them at file/class scope especially if you are working in teams for this reason: say you start with a small piece of code...

public void doSomething() {

  final double MIN_INTEREST = 0.0;

  // ...  
}

and other members of you team expand the class with whole bunch of methods and now the class is a wonderful 500 lines/50 methods giant class. Imagine the experience of an engineer who is trying to adding a new method with a constant, they will have to 1 scan the whole class looking for constants that match their need, 2 move the constant to class scope hoping that there are no conflicts with existing code and 3 also add their method.

If you instead add all constants at file/class scope to begin with, engineers have a 1 single place to look for existing constants and, 2 derive some constants from others where it makes sense. (for example if you have a constant for pi you may also want to define a new constant with a value of pi/2).

-1

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.

1
  • 1
    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
    Commented Oct 28, 2008 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.