0
private volatile FieldType field;
FieldType getField() {
    FieldType result = field;
    if (result == null) { 
        synchronized(this) {
            result = field;
            if (result == null) 
                field = result = computeFieldValue();
        }
    }
    return result;
}

As we almost all know about this is the sample code for double check idiom for lazy initialization of instance field. But i have a silly doubt here how somebody will create the singleton object of FieldType. As to call the function getField() (which create the singleton instance) you need an instance of the class but till now you don't have the instance. I am bit confused, please let me know. Thanks!!!

syb0rg
  • 8,057
  • 9
  • 41
  • 81
Trying
  • 14,004
  • 9
  • 70
  • 110
  • @FredOverflow The problem is that `static` is missing which I don't think this question answers. – Peter Lawrey Apr 28 '13 at 17:47
  • @FredOverflow thanks for your opinion. But my question is totally different. I am asking something different. – Trying Apr 28 '13 at 17:48
  • @PeterLawrey People need to stop implementing the Singleton pattern themselves when enums are so much easier and do everything right. (Or even better, scratch everything starting at "themselves".) – fredoverflow Apr 28 '13 at 17:49

2 Answers2

1

Of source the simplest singleton is an enum

enum Singleton {
    INSTANCE;
}

But in this more complicated case,

how somebody will create the singleton object of FieldType.

They have to call getField() which must be static, as does the field

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • thanks. But if you see the Effective java, Joshua Bloch mentioned exactly the same code. And specifically mentioned that `If you need to use lazy initialization for performance on an instance field, use the double-check idiom.`. Are you saying that he missed something in the book. – Trying Apr 28 '13 at 17:51
  • Eager initialization is way more performant than lazy initialization, double-checked or not. Use the lazy initialization holder class idiom, or even better enums, or even better: don't use the Singleton pattern at all. – fredoverflow Apr 28 '13 at 17:54
  • @FredOverflow thanks. I also know whatever you are trying to say but there is no harm in knowing several concepts right? So i am just trying to understand what Joshua Bloch is trying to explain in his award wining book. Hope you understand my point. It's just a humble request to you no offend. Thanks. – Trying Apr 28 '13 at 17:58
  • @Trying, which is why in the 2nd edition he suggested using `enum`s indead. Possibly because this was written after Java 5.0 was released. – Peter Lawrey Apr 28 '13 at 17:58
  • @Trying The problem is that you get people repeating the same questions based on code which has been considered bad practice for almost a decade, i.e. in many case before they even started using Java. – Peter Lawrey Apr 28 '13 at 18:00
  • @Trying Thanks for your kind words. I wasn't offended at all. – fredoverflow Apr 28 '13 at 18:16
0

As a first RULE of Singleton

FieldType getField()

should be defined as

public static FieldType getField()

so that method getField() (static method) of FiledType can be invoked, without creating instance

Of course, you need to define private constructor for FieldType (which was missing here)

sanbhat
  • 17,522
  • 6
  • 48
  • 64