472,798 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,798 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 2506
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; };
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.