Article View: borland.public.jbuilder.java.language
Article #18418Re: Global Singleton Classes?
From: Torsten Welches
Date: Sun, 28 Jun 1998 00:00
Date: Sun, 28 Jun 1998 00:00
78 lines
2975 bytes
2975 bytes
Gerry,
your code does not compile as is.
You can't reference a non-static variable or method from a static
method. Hence your mySingleton variable must be declared static. Or am
I missing something basic?
BTW - your class should start "class MySingleton {"
not "class MySingleton(){"
Torsten
On Sat, 27 Jun 1998 10:58:58 -0400, "Gerry Mantha" <gmantha@home.com>
wrote:
>Java handles the singleton model very well... example:
>
>class MySingleton(){
> MySingleton mySingleton; // defaults to null
> ... your properties....
>
> private MySingleton(){ // note the private constructor
> }
>
> public static MySingleton getMySingleton(){
> if(mySingleton == null) mySingleton = new MySingleton();
> return mySingleton;
> }
>
> public PropertyWhatever1 getPropertyWhatever1(){
> return propertyWhatever1;
> }
> public setPropertyWhatever1(PropertyWhatever1 propertyWhatever1){
> this.propertyWhatever1 = propertyWhatever1
> }
> (..... more setters and getters, or whatevers.....)
>}
>
>
>In order to use your new singleton anywhere, do this:
>
>MySingleton.getMySingleton().getPropertyWhatever1();
>
>Note that you can do the same thing with pure static members and methods
>without even making an instance. However using the true singleton model
>gives you the flexibility for your methods and other called methods the
>ability to access non static variables. JBuilder's DataModules use the
>singleton pattern as i've described here for example.
>
>Luck,
>
>Gerry
>
>
>Rob Williams wrote in message <3593F482.6CAEEFBA@robco-inc.com>...
>>I'm writing an app that needs to make some basic information available
>>to a number of frames in the application, like the date that the current
>>week began on, what location is using the program, etc. Since we all
>>know, obviously, that we aren't supposed to be doing public variables, I
>>wonder if the right thing would be to create an instance of a class
>>called AppSettings in the Application class and then get references to
>>it in any of the frames where I need some of the information. I tried
>>to look at the Calendar class as an example of how to do this but didn't
>>really learn anything; it seems to new a Gregorian Calendar in the
>>constructor.
>>
>>Thanks,
>>
>>Rob Williams
>>RobCo Incorporated
>>
>>
>
>
-------
please remove '.NO_SPAM' from e-mail adress to contact me outside the newsgroups
Technical Details
Message-ID:
<35966155.4648714@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3593F482.6CAEEFBA@robco-inc.com> <6n319c$qf87@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,b8537ec38763bb48 X-Google-Attributes: gidf74b3,public From: torsten.welches.NO_SPAM@debis.com (Torsten Welches) Subject: Re: Global Singleton Classes? Date: 1998/06/28 Message-ID: <35966155.4648714@forums.borland.com> X-Deja-AN: 366836972 References: <3593F482.6CAEEFBA@robco-inc.com> <6n319c$qf87@forums.borland.com> Organization: debis Systemhaus Newsgroups: borland.public.jbuilder.java.language