On 01.04.2016 21:38, OC wrote:
[...]
===
class Foo {
   static instance=newInstance()
}
class Bar extends Foo {
   static instance=newInstance()
}
===

it works like a charm, only -- unlike @Singleton -- it is not lazy (and if 
turned to lazy, it would not be threadsafe).

there is an easy Java trick for lazy:

class Foo {
  static getInstance() {
    return Inner.instance
  }
  private Foo(){}
  private static class Inner {
     final static instance = new Foo()
  }
}

The trick is that the inner class is only loaded if getInstance is called, thus you get lazy behaviour, that is even thread safe.

bye Jochen

Reply via email to