-1
private final int MAX = 100;

Does it have to be "private static final" instead of "private final"?

  • 1
    `final` alone makes it a constant variable. – pushkin Dec 29 '15 at 23:25
  • 1
    Making it `final` makes it a *constant*, making it `static` makes it *class-level* (does it make sense for each instance to have it's own `MAX`, or should there be one `MAX` shared by all instances). – Elliott Frisch Dec 29 '15 at 23:27

4 Answers4

0

static has nothing to do with the fact that MAX is final. It just states whether every object of the class that defines this constant has "to have its own constant MAX" (which wouldn't be really useful, since it's a constant), or if it's something that belongs to the class as a whole.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • 1
    Object level 'constant' should be rather called an immutable object property that would need to be initialized on construction of the object so it is not really a constant per say. see here as an example - they all are static: https://docs.oracle.com/javase/7/docs/api/constant-values.html – rgasiore Dec 29 '15 at 23:42
0

static means this variable is a property of a class rather than of an object. For constants it is likely desirable to have it defined as static as they don't normally change/morph its values across different objects of given class - it is better to have one property created that is a class level rather than multiplying it across different objects of the class that would have the same value. You save memory and you don't have to create objects to access their value.

Here is more information about static: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html and here is an answer to the question on how to define constants What is the best way to implement constants in Java?

Community
  • 1
  • 1
rgasiore
  • 140
  • 7
0

The keyword private is referring to the visibility of the field/member(variable), final means once defined will never change its value, static means you don't actually need an object in order to use this value.


Conclusion:

private final int can be a field/member of an object but you cant access to it from outside since is private. therefore you can never use this as a constant.

if you do:

  • public final int MAX then MAX can be accessed through an object properties eg foo.MAX
  • private final int MAX then MAX can be accessed through getters eg foo.getMax()
  • public static final int MAX then MAX can be accessed through Class eg Integer.MAX
  • private static final int MAX then MAX can be accessed internally in the class eg Integer.MAX
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

final alone makes it constant, static makes it use the same allocated space for every instance of the class it is in. When defining constants in Java, you should use both of them (that's probably you're having this confusion). Why you should use them both for defining constants? Because then you wouldn't have to create an instance of the class they're defined in AND if you do create an instance, they fill the same allocated space, thus you don't waste memory.

DDsix
  • 1,966
  • 1
  • 18
  • 23