Thread overview
Static Initializors
Jun 21, 2004
Benji Smith
Jun 21, 2004
Hauke Duden
Jun 22, 2004
Benji Smith
Jun 22, 2004
Trejkaz Xaoza
Jun 22, 2004
Benji Smith
Jun 22, 2004
Matthew
June 21, 2004
I've been writing a lot of java code recently, and I've found myself wanting something analagous to a constructor, but for the static data in a class. Instead, I've been littering all of my static methods with a call to a private static initialize() method.

It looks like this:

public static void myFunciton() {
	if (!MyClass.initialized) {
		MyClass.initialize();
	}
	...do stuff...
}

public static void myOtherFunciton() {
	if (!MyClass.initialized) {
		MyClass.initialize();
	}
	...do stuff...
}

private static void initialize() {
	...set up static variables, connect to resources, etc...
}

Much in the same way that a constructor fires on the creation of new objects, I'd like to have a compiler-recognized static initialization function, automatically called (only once) whenever any static method (or static field?) is called for the first time.

Hmmmmm.....come to think of it, I've been creating classes full of static methods when I should be using a singleton object. Is there much of an argument for adding the notion of singletons into the language itself, or do you think singletons should be implemented in a library?

Nevertheless, even if I should be using a singleton for the stuff I'm doing right now, what do you think of the concept of an automatically-called static initializor function?

--Benji Smith
June 21, 2004
Benji Smith wrote:
> Nevertheless, even if I should be using a singleton for the stuff I'm
> doing right now, what do you think of the concept of an
> automatically-called static initializor function?

My thought is that it's already in there! ;)

See "Static constructors" in
http://www.digitalmars.com/d/class.html

Hauke
June 22, 2004
On Tue, 22 Jun 2004 01:06:20 +0200, in digitalmars.D you wrote:

>My thought is that it's already in there! ;)
> 
> See "Static constructors" in http://www.digitalmars.com/d/class.html
> 
> Hauke

Hot damn. That's exactly what I was thinking of. I'm glad to see that it already exists (but I don't ever remember any discussion of it here.

One thing that piques my curiosity, though, is this phrase:

> static this() is called by the startup code before main() is called

If that's the case, what about classes loaded from a DLL during runtime. Obviously, their static constructors can't be called before the beginning of the main() function. I'd expect all of the static initializers from all of the classes in the loaded library to be called immediately after the library was loaded. Is that the case?

--Benji Smith
June 22, 2004
In article <o6ved0li4m6i4t0m835bfhfb2ali4tbi3i@4ax.com>, Benji Smith says...
>One thing that piques my curiosity, though, is this phrase:
>
>> static this() is called by the startup code before main() is called
>
>If that's the case, what about classes loaded from a DLL during runtime. Obviously, their static constructors can't be called before the beginning of the main() function. I'd expect all of the static initializers from all of the classes in the loaded library to be called immediately after the library was loaded. Is that the case?
>
>--Benji Smith

Benji,
That's exactly how it happpens, provided you build your .dll correctly.

Take a look here, there's an example of how DllMain is composed for .dll's:

http://www.digitalmars.com/d/windows.html

(pipes inserted to preserve spacing)
|case DLL_PROCESS_ATTACH:
|   gc_init();			// initialize GC
|   _minit();			// initialize module list
|   _moduleCtor();		// run module constructors
|   _moduleUnitTests();		// run module unit tests
|   break;

From my own observations, the call to _moduleCtor() pretty much takes care of
the module's construction needs.  This includes any "static this(){}" calls as
well.

Happy hunting.


June 22, 2004
Not meaning to be annoying, but this is niggling me at the moment...

You're not implying that such a thing doesn't exist in Java, are you?  Because
obviously Java can do:
    static {
        //...
    }

Which works the same as D's:
    static init() {
        //...
    }

Your post doesn't make it overly clear whether the code you're writing is Java or D, though it looks like Java at first glance which is why I'm a little concerned about the way you did this. :-)

TX

In article <aroed09oko3967j2r0lho1n67ggf0te194@4ax.com>, Benji Smith says...
>
>I've been writing a lot of java code recently, and I've found myself wanting something analagous to a constructor, but for the static data in a class. Instead, I've been littering all of my static methods with a call to a private static initialize() method.
>


June 22, 2004
"Benji Smith" <dlanguage@xxagg.com> wrote in message news:aroed09oko3967j2r0lho1n67ggf0te194@4ax.com...
> I've been writing a lot of java code recently, and I've found myself wanting something analagous to a constructor, but for the static data in a class. Instead, I've been littering all of my static methods with a call to a private static initialize() method.
>
> It looks like this:
>
> public static void myFunciton() {
> if (!MyClass.initialized) {
> MyClass.initialize();
> }
> ...do stuff...
> }
>
> public static void myOtherFunciton() {
> if (!MyClass.initialized) {
> MyClass.initialize();
> }
> ...do stuff...
> }
>
> private static void initialize() {
> ...set up static variables, connect to resources, etc...
> }
>
> Much in the same way that a constructor fires on the creation of new objects, I'd like to have a compiler-recognized static initialization function, automatically called (only once) whenever any static method (or static field?) is called for the first time.

They exist in both D and Java.

> Hmmmmm.....come to think of it, I've been creating classes full of static methods when I should be using a singleton object. Is there much of an argument for adding the notion of singletons into the language itself, or do you think singletons should be implemented in a library?
>
> Nevertheless, even if I should be using a singleton for the stuff I'm doing right now, what do you think of the concept of an automatically-called static initializor function?

IMHO, D's static initialiser functionality obviates most needs for the much abused Singleton pattern. In most cases you can, and should, simply use free functions from a module, within which reside the shared "static" data.



June 22, 2004
On Tue, 22 Jun 2004 05:38:01 +0000 (UTC), Trejkaz Xaoza
<trejkaz@xaoza.net> wrote:

>Not meaning to be annoying, but this is niggling me at the moment...
>
>You're not implying that such a thing doesn't exist in Java, are you?  Because obviously Java can do:
>    static {
>        //...
>    }

Yeah, the code I was writing was Java. And I actually didn't know about this construct. How could I possibly have missed that? I've been programming Java for two years (after several years programming perl, VB, and various web languages).

Thanks for the tip.

Please resume your normal non-idiotic, non-time-wasting, newsgroup activities....

Sheepishly,

--BenjiSmith