java - private static final vs private final -


this question has been asked here. per answer :

private final int number = 10; 

if cannot change, there no point having 1 copy per instance.

my doubt if instance of class created, once day , lasts around few seconds. idea keep int(in case object) in memory?

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

how store information depends on intended used for.

there few approaches might take:

private static final

this choice if value never modified during lifetime of application. means, when you're creating multiple instances, storing particular variable once.

private final

this meant times when value might take on different values different instances of object, specific instance not have it's value modified throughout object's life time.

if you're looking @ might take on different values on range of time, might of interest you.

public static int getnumber(){...}

another approach might consider have static method return value after. makes easy deal changes in value, need consider effect of such change throughout lifetime of given instance.

hope helps...


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -