473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

static vars declared with class instantiation

Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that references
Class1 ?

Or ... something else ...

Jun 2 '06 #1
10 1736

John A Grandy wrote:
Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that references
Class1 ?

Or ... something else ...


I'm assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It's the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)

Jun 2 '06 #2
Hmmm ...

So, I'm kindof guessing here, but seems that Singletons might be much less
relevant in .NET compared with , say , Java ?

A .NET class with a static constructor (or has all its data contained in
static vars that are instantiated on declaration) effectively replaces a
Singleton ... because any instantiation-related work is guaranteed to only
be performed once -- on first reference of the class.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...

John A Grandy wrote:
Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that
references
Class1 ?

Or ... something else ...


I'm assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It's the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)

Jun 3 '06 #3
Not surprisingly .. Jon has a write up about it here
http://www.yoda.arachsys.com/csharp/...fieldinit.html

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...

John A Grandy wrote:
Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that
references
Class1 ?

Or ... something else ...


I'm assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It's the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)

Jun 3 '06 #4
<"John A Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
So, I'm kindof guessing here, but seems that Singletons might be much less
relevant in .NET compared with , say , Java ?

A .NET class with a static constructor (or has all its data contained in
static vars that are instantiated on declaration) effectively replaces a
Singleton ... because any instantiation-related work is guaranteed to only
be performed once -- on first reference of the class.


Yes (for suitably detailed values of "on first reference") but I'm not
sure why you think that's different to Java, which has broadly the same
semantics.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 3 '06 #5

John A Grandy wrote:
Hmmm ...

So, I'm kindof guessing here, but seems that Singletons might be much less
relevant in .NET compared with , say , Java ?

A .NET class with a static constructor (or has all its data contained in
static vars that are instantiated on declaration) effectively replaces a
Singleton ... because any instantiation-related work is guaranteed to only
be performed once -- on first reference of the class.


With one hugely important caveat: you can't get a reference to a static
class, so you can't pass it as an argument, return it as a method
result, store it in a collection, etc. Sometimes you want to do these
things with a singleton class. That's why I usually prefer the
singleton pattern to a static class.

As well, if I make something a static class and then later decide my
design was wrong, I'm in for a nightmare. If I make it a singleton and
then decide, for example, that I need to inherit from it and benefit
from polymorphism, well that's easy to fix.

Jun 3 '06 #6

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"John A Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
So, I'm kindof guessing here, but seems that Singletons might be much
less
relevant in .NET compared with , say , Java ?

A .NET class with a static constructor (or has all its data contained in
static vars that are instantiated on declaration) effectively replaces a
Singleton ... because any instantiation-related work is guaranteed to
only
be performed once -- on first reference of the class.


Yes (for suitably detailed values of "on first reference") but I'm not
sure why you think that's different to Java, which has broadly the same
semantics.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 4 '06 #7
Hmmm ... well, it looks like I've obtained a misunderstanding of how Java
handles static vars from a design patterns book I won't name.

This book states that the traditional Singleton will have its static vars'
assignment constructors invoked on load of the class ... which I understand
to mean load of the class' assembly.

I'm referring to the example I gave above (which Bruce correctly augmentd).
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"John A Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
So, I'm kindof guessing here, but seems that Singletons might be much
less
relevant in .NET compared with , say , Java ?

A .NET class with a static constructor (or has all its data contained in
static vars that are instantiated on declaration) effectively replaces a
Singleton ... because any instantiation-related work is guaranteed to
only
be performed once -- on first reference of the class.


Yes (for suitably detailed values of "on first reference") but I'm not
sure why you think that's different to Java, which has broadly the same
semantics.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 4 '06 #8
<"John A Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
Hmmm ... well, it looks like I've obtained a misunderstanding of how Java
handles static vars from a design patterns book I won't name.

This book states that the traditional Singleton will have its static vars'
assignment constructors invoked on load of the class ... which I understand
to mean load of the class' assembly.


There's no such concept of "assembly" in Java, so that can't be right.

However, the semantics of when a type's initialization occurs is in the
Java language specification. From section 12.4.1:

<quote>
A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:

* T is a class and an instance of T is created.
* T is a class and a static method declared by T is invoked.
* A static field declared by T is assigned.
* A static field declared by T is used and the reference to the
field is not a compile-time constant (§15.28). References to compile-
time constants must be resolved at compile time to a copy of the
compile-time constant value, so uses of such a field never cause
initialization.
</quote>

In other words, it's pretty similar to the C# behaviour when there's a
static constructor present. (When there's no static constructor present
in C#, the CLR has more discretion in terms of exactly when type
initialization occurs.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 4 '06 #9
Once initialized , a static ref to a type retains its instantiated object in
memory until the containing app domain is recycled (either as part of worker
process recycle , or otherwise ) ...

For an asp.net app , I want to use the singleton strictly within the
processing of a single page request ...

( Complex conditional logic in the page processing means that the "Instance"
method or property on the singleton won't necessarily be called , and so the
private member won't necessarily be instantiated. )

If I provide a function that can be called to set the static ref to null (
to dispose of the referred to object before page unload ) , what
complications are there to proper design of a scalable singleton ?
Or is a singleton not the best solution for my design goal .... ?
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"John A Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
Hmmm ... well, it looks like I've obtained a misunderstanding of how Java
handles static vars from a design patterns book I won't name.

This book states that the traditional Singleton will have its static vars'
assignment constructors invoked on load of the class ... which I
understand
to mean load of the class' assembly.


There's no such concept of "assembly" in Java, so that can't be right.

However, the semantics of when a type's initialization occurs is in the
Java language specification. From section 12.4.1:

<quote>
A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:

* T is a class and an instance of T is created.
* T is a class and a static method declared by T is invoked.
* A static field declared by T is assigned.
* A static field declared by T is used and the reference to the
field is not a compile-time constant (§15.28). References to compile-
time constants must be resolved at compile time to a copy of the
compile-time constant value, so uses of such a field never cause
initialization.
</quote>

In other words, it's pretty similar to the C# behaviour when there's a
static constructor present. (When there's no static constructor present
in C#, the CLR has more discretion in terms of exactly when type
initialization occurs.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 7 '06 #10
<"John A Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
Once initialized , a static ref to a type retains its instantiated object in
memory until the containing app domain is recycled (either as part of worker
process recycle , or otherwise ) ...
Yup.
For an asp.net app , I want to use the singleton strictly within the
processing of a single page request ...


That doesn't sound like it's really a singleton then. It's just
something associated with the request. That's the association you want
to make, along with lazy instantiation presumably. I wouldn't start
trying to make things "real" singletons and then modifying them.

Unfortunately I don't know enough ASP.NET and don't have time to
investigate at the minute to know exactly where you should hook things
in, but I wouldn't have thought it would be too hard.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 8 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Connell Gauld | last post by:
Hi, I'm using VC++. I have a class called exClass defined in exClass.h exClass has a static variable, declared like this in the public section. static int exStaticVar; Below the class definition...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
10
by: Chris Clement | last post by:
I didn't write this code so I'm not sure what is going on with this error. checkedListBox1 is a public object. Why does the error say that it is expecting a 'class' in a for loop? Is it because...
8
by: Robert A Riedel | last post by:
I have an application that requires a DLL and an executable that uses the DLL, both of which were implemented in Visual C++ using unmanged code. Both the executable and the DLL are linked with...
6
by: Maya | last post by:
Hello guys, in C#, is using "static" would be the most proper way to get around calling methods located in different classes? for instance, a method caller in class A wouldn't see a method in class...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.