468,310 Members | 1,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,310 developers. It's quick & easy.

static members

Hi, can someone please explain to me how a static member of a class works.
For example if I have:
public Class1
public static Class2 myClass2 = new Class2;
public static Int16 thisValue = 200;
public static string thisString = "This string value";
public static boolean thisBool = True
....

if Class1 is never instantiated what value do these variables take on and
exactly when do they get these values? In other words when is Class2
instantiated?

If Class1 is instantiated and then terminated what value do these variables
have. Do the variables have their values restored immediately after the
termination of the Class1 object or does it wait for GC?

Thanks,
Fred
Nov 15 '05 #1
4 2393
Hi

Classes can contain static member data and member
functions. When a data member is declared as static, only
one copy of the data is maintained for all objects of the
class.

Static data members are not part of objects of a given
class type; they are separate objects. As a result, the
declaration of a static data member is not considered a
definition.

HTH
Ravikanth[MVP]

-----Original Message-----
Hi, can someone please explain to me how a static member of a class works.For example if I have:
public Class1
public static Class2 myClass2 = new Class2;
public static Int16 thisValue = 200;
public static string thisString = "This string value"; public static boolean thisBool = True
....

if Class1 is never instantiated what value do these variables take on andexactly when do they get these values? In other words when is Class2instantiated?

If Class1 is instantiated and then terminated what value do these variableshave. Do the variables have their values restored immediately after thetermination of the Class1 object or does it wait for GC?

Thanks,
Fred
.

Nov 15 '05 #2
The static members of Class1 are instantiated as soon as your program enters
a method that either references one of the static members of Class1 or
instantiates an object of type Class1. This is when Class2 is instantiated
by the static member initializer.

The Class1 member variables take on the values specified in the initializers
as shown below, and retain those values until they are changed. It doesn't
matter how many instances of Class1 you create or destroy--the static
members are stored independently of any instances of the class.
"fred" <noThanks@TrySomeoneElse> wrote in message
news:ec**************@TK2MSFTNGP11.phx.gbl...
Hi, can someone please explain to me how a static member of a class works.
For example if I have:
public Class1
public static Class2 myClass2 = new Class2;
public static Int16 thisValue = 200;
public static string thisString = "This string value";
public static boolean thisBool = True
...

if Class1 is never instantiated what value do these variables take on and
exactly when do they get these values? In other words when is Class2
instantiated?

If Class1 is instantiated and then terminated what value do these variables have. Do the variables have their values restored immediately after the
termination of the Class1 object or does it wait for GC?

Thanks,
Fred

Nov 15 '05 #3
Bret Mulvey [MS] <br***@online.microsoft.com> wrote:
The static members of Class1 are instantiated as soon as your program enters
a method that either references one of the static members of Class1 or
instantiates an object of type Class1. This is when Class2 is instantiated
by the static member initializer.


Note, however, that unless you have a static constructor, the runtime
may decide to set those values at any time before any of the static
members are referenced - that could (and usually is) before they would
be set normally (eg at the start of the first method which *might*
reference a member) or it could be *later* than they'd be set normally
(ie creating a new instance of the class wouldn't necessarily trigger
them being set, and nor would calling a method).

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Thanks Jon, your article on "beforefieldinit" explained it very well.

Fred

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bret Mulvey [MS] <br***@online.microsoft.com> wrote:
The static members of Class1 are instantiated as soon as your program enters a method that either references one of the static members of Class1 or
instantiates an object of type Class1. This is when Class2 is instantiated by the static member initializer.


Note, however, that unless you have a static constructor, the runtime
may decide to set those values at any time before any of the static
members are referenced - that could (and usually is) before they would
be set normally (eg at the start of the first method which *might*
reference a member) or it could be *later* than they'd be set normally
(ie creating a new instance of the class wouldn't necessarily trigger
them being set, and nor would calling a method).

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information.

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

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Scott J. McCaughrin | last post: by
15 posts views Thread by Samee Zahur | last post: by
13 posts views Thread by Adam H. Peterson | last post: by
3 posts views Thread by Mauzi | last post: by
6 posts views Thread by Matt | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.