3

Does any one know differences of this two approach?

public class SingleTone {

    private static final instance = new SingleTone();

    private SingleTone() {
    }
    public [static] SingleTone getinstance() {
        return instance;
    }
} 

and

public class SingleTone {

    private static instance = new SingleTone();

    private SingleTone() {
    }
    public  [static] SingleTone getinstance() {
        return instance;
    }
} 

I know final static variables are thread-safe however i can't find any problem with non-final one. (NOTE: I'm seeking the differences of final static variables with non-final static variables so please don't suggest how to implement the Singleton design pattern. I know there is an Enum approach.)

Edited: I missed static keyword on method now its fixed!

Morteza Adi
  • 2,413
  • 2
  • 22
  • 37
  • 2
    Not relevant to the question, but more of a FYI: the pattern is actually called "Singleton". – Saulo Silva Mar 18 '13 at 19:33
  • I intentinally named class SingleTone i know the pattern name is Singleton and you could see i used singleton at the bottom of my question – Morteza Adi Mar 18 '13 at 19:50

3 Answers3

2

There isn't much difference except possible compiler optimizations. In either case, because it's private with no setter, there's nothing that can modify it, so it doesn't particularly matter if it's final or not. However, in both cases, your getInstance() method needs to be declared static.

Marvo
  • 17,845
  • 8
  • 50
  • 74
Adrian
  • 42,911
  • 6
  • 107
  • 99
  • 1
    Something else in the class could change it meaning that any published instances of it would have a stale reference. I would always recommend using final if you aren't using an IoC container and having that manage your singletons. – James Mar 18 '13 at 19:35
  • Agreed - it's best practice to declare it final, but if you control the class and don't define any code that modifies the property, then it is *effectively* final. – Adrian Mar 18 '13 at 19:36
  • True, also there is so much else wrong with singleton, e.g., you'll never be able to create one as `getinstance()` isn't static and it has a private constructor. :) – James Mar 18 '13 at 19:38
  • thanks I missed static keyword on method now its fixed! as i said i'm not implementing singleton if i like to use enum for that it is just an example to learn differences of final static and none final static variables. Can elaborate more on stale reference ?? how final static can prevent this and why none final variable can't?? – Morteza Adi Mar 18 '13 at 19:59
  • 1
    A final variable, once it is set, can never be changed. So, if something has a reference to it, it will always be a correct reference - there's no chance that it has changed since it got the reference. If it's not final, there's a chance that something got a reference to it, but it was then changed, meaning that the old reference is now "stale" (invalid). – Adrian Mar 18 '13 at 20:03
2

The final modifier means that nothing else will be assigned to that field. That is you can't use the equals (=) operator on it. While the static modifier means that the variable is part of the class and not of the instance, so there will be only one of those no matter how many instances you have.

The first approach then makes the reference thread-safe because we know that the value won't change after initialization. However, if we have a final structure we need to make sure that the contents are synchronized as well. See this question for more info.

Community
  • 1
  • 1
Sednus
  • 2,095
  • 1
  • 18
  • 35
0

final will not give any special performance improvement in your case.

We use final for clear design approach. As per Josh Bloch's "design for inheritance or prohibit it" rule of thumb, should probably use final more often for classes in general, not only for this case.

Nilay Shah
  • 31
  • 5