Categories
attributes final java private static

private final static attribute vs private final attribute

331

In Java, what’s the difference between:

private final static int NUMBER = 10;

and

private final int NUMBER = 10;

Both are private and final, the difference is the static attribute.

What’s better? And why?

4

  • 74

    private final static -> create this variable only once. private final -> create this variable for every object. First one saves memory, go for it.

    May 26, 2015 at 9:50


  • 5

    final static means this variable is a constant and only associates with the class itself, i.e. “one constant variable per class” while final means “one constant variable per instance”. As a result, you cannot put a final static variable in the class’ constructor since the constructor involves in new an instance. (You can try it yourself and will get an error)

    Dec 29, 2016 at 9:30

  • 1

    By “cannot put a final static variable in the class’ constructor” I mean one cannot initialize a final static variable in the constructor, the only way is to use a static initializer 🙂

    Dec 30, 2016 at 3:15

  • 2

    @user1923551 The effect is inversed for stuff that is only needed for a limited amounts of time in big applications, in memory scarce applications or when using a singleton. Having static stuff in every class will reserve a (huge) chunk of memory for stuff that isn’t needed. It can also be a memory leak to declare static final objects or collections.

    Jan 2, 2017 at 10:34


43

A static variable stays in the memory for the entire lifetime of the application, and is initialised during class loading. A non-static variable is being initialised each time you construct a new object. It’s generally better to use:

private static final int NUMBER = 10;

Why? This reduces the memory footprint per instance. It possibly is also favourable for cache hits. And it just makes sense: static should be used for things that are shared across all instances (a.k.a. objects) of a certain type (a.k.a. class).

4

  • a static variable is also created at runtime. Therefore you can use said variable or method before the object is created.

    – bobby

    Sep 12, 2009 at 19:58

  • 14

    By Java coding convention the name of a static final variable should be all uppercase.

    – starblue

    Sep 13, 2009 at 6:27

  • @Martijn Courteaux, how about a situation where a class will be used once during the lifetime of the app! private final int will be removed from memory when the instance will be GC’ed whereas private static final int will remain in memory throughout the lifespan of that app. What do you suggest in the above scenario?

    – MANN

    Jun 30, 2017 at 22:01

  • @MANN: This is highly theoretical. There is literally no useful use case scenario for that. This could be beneficial if you have like 50000 int vars in a class. Even in this case, this would save up 200kb of memory. Since we are talking Java, this seems totally irrelevant. In case of memory critical devices, a decent C or C++ compiler would inline those integer values always, eliminating the need for freeing up memory completely.

    Jul 3, 2017 at 21:51

36

For final, it can be assigned different values at runtime when initialized.
For example

class Test{
  public final int a;
}

Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t2.a = 20; //fixed

Thus each instance has different value of field a.

For static final, all instances share the same value, and can’t be altered after first initialized.

class TestStatic{
      public static final int a = 0;
}

TestStatic t1  = new TestStatic();
t1.a = 10; // ERROR, CAN'T BE ALTERED AFTER THE FIRST 
TestStatic t2  = new TestStatic();
t1.a = 20;   // ERROR, CAN'T BE ALTERED AFTER THE FIRST INITIALIZATION.

8

  • 94

    This will not compile! A final variable must either be assigned a value, or have a value assigned in its constructors. This answer would be correct if 2 constructors were given, with each assigning ‘a’ to a different value.

    – MattC

    Apr 20, 2013 at 18:04

  • 16

    Confirming, this will not compile. As expressed above, final instance variable’s must be instantiated before the constructor finishes, and final class variables must be instantiated before the class has been created (you can use a static block). Why has this got so many upvotes?

    Aug 31, 2014 at 13:17

  • as MattC pointed out, you cannot assign to a final variable after creating that object – in fact, you cannot even create an object without giving values to its final variables…

    Jan 30, 2015 at 22:08

  • Just in case anyone stumbles on this, please follow MattC’s answer.

    – Faz

    Sep 10, 2015 at 8:12

  • 4

    Please DELETE this answer

    Aug 26, 2019 at 8:18