473,320 Members | 2,145 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,320 software developers and data experts.

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 2535
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
6
by: Matt | last post by:
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.