Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0833118109 for ; Sat, 2 Apr 2016 18:01:54 +0000 (UTC) Received: (qmail 91825 invoked by uid 500); 2 Apr 2016 18:01:53 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 91799 invoked by uid 500); 2 Apr 2016 18:01:53 -0000 Mailing-List: contact users-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.apache.org Delivered-To: mailing list users@groovy.apache.org Received: (qmail 91788 invoked by uid 99); 2 Apr 2016 18:01:53 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 02 Apr 2016 18:01:53 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 630311A05C8 for ; Sat, 2 Apr 2016 18:01:53 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.299 X-Spam-Level: X-Spam-Status: No, score=0.299 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id 0fawiFwuHyhQ for ; Sat, 2 Apr 2016 18:01:51 +0000 (UTC) Received: from smtp.czechia.com (smtp1.czechia.com [217.198.120.39]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTPS id 2AE0F5F23D for ; Sat, 2 Apr 2016 18:01:49 +0000 (UTC) Received: from [10.0.0.123] (31.4.broadband2.iol.cz [83.208.4.31]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: ocs@ocs.cz) by smtp.czechia.com (Postfix) with ESMTPSA id B646E2009CC28 for ; Sat, 2 Apr 2016 20:01:41 +0200 (CEST) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: Hierarchy of singletons howto? From: OC In-Reply-To: <56FFFF14.3080707@gmx.org> Date: Sat, 2 Apr 2016 20:01:55 +0200 Content-Transfer-Encoding: quoted-printable Message-Id: References: <1A523D17-D575-461A-9274-3E3D831607DD@ocs.cz> <56FFFF14.3080707@gmx.org> To: users@groovy.apache.org X-Mailer: Apple Mail (2.1510) Jochen, thanks a lot, nice trick! If you can spare a moment, might you perhaps check my =93static = propertyMissing in an interface=94 message of yesterday? The question is = whether the approach one can rely on to remain in future Groovy = versions; that it works all right in current ones I have tested :) Thanks and all the best, OC On 2. 4. 2016, at 19:19, Jochen Theodorou wrote: > On 01.04.2016 21:38, OC wrote: > [...] >> =3D=3D=3D >> class Foo { >> static instance=3DnewInstance() >> } >> class Bar extends Foo { >> static instance=3DnewInstance() >> } >> =3D=3D=3D >>=20 >> it works like a charm, only -- unlike @Singleton -- it is not lazy = (and if turned to lazy, it would not be threadsafe). >=20 > there is an easy Java trick for lazy: >=20 > class Foo { > static getInstance() { > return Inner.instance > } > private Foo(){} > private static class Inner { > final static instance =3D new Foo() > } > } >=20 > The trick is that the inner class is only loaded if getInstance is = called, thus you get lazy behaviour, that is even thread safe. >=20 > bye Jochen >=20