5

This question has been asked here. As per the answer :

private final int NUMBER = 10;

If it cannot change, there is no point having one copy per instance.

My doubt is what if instance of the class is created, say once a day and it lasts around a few seconds. Is it good idea to keep the int(in some case object) in memory?

Assuming, there can be many (20-30) such objects.

Community
  • 1
  • 1
codingenious
  • 8,385
  • 12
  • 60
  • 90
  • 6
    Your question right now is dealing with premature optimization. If you really want to question this, then you need to come up with realistic numbers for some existing memory-limited embedded device. – skiwi Mar 24 '14 at 11:15
  • 3
    Agree with skiwi. Does it matter? Who cares? Garbage collection is more than capable of dealing with such "waste". – Brian Mar 24 '14 at 11:17
  • 1
    Just think about it: `private` modifier means that only (instances of) this class will be able to access and modify the property. `final` means that the value is not going to be modifiable after object construction. So unless you are setting the value in your constructor (last permissible moment) the value is gong to be the same for all instances of the class - i.e you may as well declare it `static` – Germann Arlington Mar 24 '14 at 11:23
  • In fact, considering the memory consumption is indeed somewhat premature optimization. It should not be taken into account. What indeed is much more important is the design question. Where does the field belong to? To the class or to each instance? For example: Is it a constant that just gives a magic value, which is used in the context of this class, a name? Then make it static; it belongs to the class. Having said this, I think nearly all final fields that are initialized at declaration, are such constants. – Seelenvirtuose Mar 24 '14 at 12:18

2 Answers2

4

How you store the information depends very much on what it is intended to be used for.

There are a few approaches you might take:

private static final

This is a good choice if the value will never be modified during the lifetime of the application. It means, when you're creating your multiple instances, you are only actually storing this particular variable ONCE.

private final

This is meant for those times when the value might take on different values for different instances of your object, but any specific instance will not have it's value modified throughout the object's life time.

If you're looking at something which might take on different values over a range of time, then this might be of interest to you.

public static int GetNumber(){...}

Another approach you might consider is to have a static method return the value you are after. This makes it easy to deal with changes in the value, but you also need to consider the effect of such a change throughout the lifetime of any given instance.

Hope that helps...

Martin Milan
  • 6,346
  • 2
  • 32
  • 44
  • Static vs non-static is explained well here, but how does it answer the question? – skiwi Mar 24 '14 at 11:22
  • 1
    I have pointed out that the choice of how to store the value depends very much on what the value is being used to represent - which it does. I've also shown three approaches, and described when each is suitable... As for a definitive answer, it depends on whether this value changes in respect to object instances - hence my explanation. – Martin Milan Mar 24 '14 at 11:31
  • He might also wish reflect that it is unwise to hold large objects in static final variables, as they consume memory resource. Ints aren't going to be a problem though... – Martin Milan Mar 24 '14 at 11:33
2

Regarding private final int number, your claim that

If it cannot change, there is no point having one copy per instance

is absolutely wrong. Consider this code, typical of an immutable class:

private final int number;

public MyClass(int number) {
    this.number = number;
}

There are many instances of this pattern in the JDK, and in production code classes around the globe.


To answer your question about performance, I doubt you could measure the difference between using static vs instance constants.

Bohemian
  • 412,405
  • 93
  • 575
  • 722