0

Does it matter whether I declare a class constant as static or not?

public class MyClass {
    private final static int statConst = 1;
    private final int nonStatConst = 2;
}

statConst and nonStatConst can never change since they are final, so even nonStatConst will be the same in every instance. Does it matter whether or not I make them static?

(I realise that there would be a difference with just private final int otherConst;)

Andreas
  • 7,470
  • 10
  • 51
  • 73

5 Answers5

4

The only way it makes a difference is if you want to reference the member from a static context; in other words, you don't have a particular instance of the class to work with. In such a case, you would need the variable to be static.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Ah, I didn't know that. So is there any advantage to having the non-static final? – Andreas Sep 09 '13 at 08:12
  • Usually you'd use a non-static final in the case where you're going to set the value in the constructor. Setting it to a constant, in an initialiser, makes it a bit pointless. – Dawood ibn Kareem Sep 09 '13 at 08:13
  • Also it's infinitely easier to call ClassName.CONSTANT than finding a instance or creating one. – Thihara Sep 09 '13 at 08:19
2

Yes,It matters.

nonStatConst is belongs to specific instance.

statConst shared across all instances.

And static context also matters while accessing.

Coming to final If you declared like so ,the field you declared in your class must be initialized before the constructor finishes.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • I know what `static` means. But in the case of `final`, I can't understand what difference it would make. Couldn't an optimizing compile change all `private final int` to `private final static int` without affecting any behaviour? – Andreas Sep 09 '13 at 08:11
  • 2
    Doesn't answer the question. – Dawood ibn Kareem Sep 09 '13 at 08:12
  • Yes,rephrased with `final` too,please have a look. – Suresh Atta Sep 09 '13 at 08:18
  • @Andreas: I'm pretty sure, the compiler will not do this. 1. It's not an optimization relevant to real programs as every programmer can do it easily themselves. 2. I *believe* the compiler is not allowed to do such an optimization. – maaartinus Sep 12 '13 at 06:11
1

The difference is, that space will be allocated for nonStatConst in each instance seperately. For the static statconst space will be allocated only once.

jboi
  • 11,324
  • 4
  • 36
  • 43
0

If it is static , you will have only one constant created, that is associated with the class, If it is non static for each object the constant will be newly created. If it is non static you cannot access it with out creating Objects for the class

upog
  • 4,965
  • 8
  • 42
  • 81
0

There are two things to consider.

  1. If you don't make the variable static, every instance of MyClass that get's created will allocate new memory space for each variable (nonStatConst). Conversely no matter how many instances of MyClass we have, statConst will only allocate memory space once.

  2. If nonStatConst isn't static, you must instantiate MyClass to be able to access it.

black hole
  • 246
  • 2
  • 6