473,883 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between private static and public static.

Hi all,
can anyone tell me differene between public static and private static
method.

how they are allocated and access?.

thanks in advance.

Jan 3 '07 #1
15 34627
It's simply a matter of accessibility. Public static members will be
available to any assembly that has a reference to the assembly where the
static member is declared.. Private static members will only be accessible
from within the class. You access them via the type, rather than making
instances of the type they are declared in. They are allocated on the stack
or heap, depending on whether they are value types or reference types (as
all types are).
"archana" <tr************ **@yahoo.comwro te in message
news:11******** **************@ k21g2000cwa.goo glegroups.com.. .
Hi all,
can anyone tell me differene between public static and private static
method.

how they are allocated and access?.

thanks in advance.

Jan 3 '07 #2
I believe that the main difference is that the private static methods are
only accessible by other methods in the class.
Jan 3 '07 #3
Scott M. <s-***@nospam.nosp amwrote:
It's simply a matter of accessibility. Public static members will be
available to any assembly that has a reference to the assembly where the
static member is declared..
Yes.
Private static members will only be accessible
from within the class.
And nested classes within that class.
You access them via the type, rather than making
instances of the type they are declared in. They are allocated on the stack
or heap, depending on whether they are value types or reference types (as
all types are).
No - static variables are *always* allocated on the stack, whether
they're value types or reference types. (Just another example of where
the oversimplificat ion fails.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '07 #4
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Scott M. <s-***@nospam.nosp amwrote:
>It's simply a matter of accessibility. Public static members will be
available to any assembly that has a reference to the assembly where the
static member is declared..

Yes.
>Private static members will only be accessible
from within the class.

And nested classes within that class.
>You access them via the type, rather than making
instances of the type they are declared in. They are allocated on the stack
or heap, depending on whether they are value types or reference types (as
all types are).

No - static variables are *always* allocated on the stack, whether
they're value types or reference types. (Just another example of where
the oversimplificat ion fails.)

No, static variable are *never* allocated on the stack, they are allocated on a per
"Applicatio n Domain" special heap (called the HighFrequencyHe ap). How could they ever be
shared when stack allocated?

Willy.

Jan 3 '07 #5
Willy Denoyette [MVP] wrote:
No - static variables are *always* allocated on the stack, whether
they're value types or reference types. (Just another example of where
the oversimplificat ion fails.)

No, static variable are *never* allocated on the stack, they are allocated on a per
"Applicatio n Domain" special heap (called the HighFrequencyHe ap). How could they ever be
shared when stack allocated?
Doh doh doh, that is of course exactly what I meant :) Fortunately the
referenced page has it the right way round - although it needs updating
a bit to handle local variables captured by anonymous methods...

Jon

Jan 3 '07 #6
Jon Skeet [C# MVP] <sk***@pobox.co mwrote:
Willy Denoyette [MVP] wrote:
No - static variables are *always* allocated on the stack, whether
they're value types or reference types. (Just another example of where
the oversimplificat ion fails.)
No, static variable are *never* allocated on the stack, they are allocated on a per
"Applicatio n Domain" special heap (called the HighFrequencyHe ap). How could they ever be
shared when stack allocated?

Doh doh doh, that is of course exactly what I meant :) Fortunately the
referenced page has it the right way round - although it needs updating
a bit to handle local variables captured by anonymous methods...
Except it turns out I didn't even reference the page. I need more
sleep!

http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '07 #7
Mark R. Dawson <Ma*********@di scussions.micro soft.comwrote:
Hi Jon,
in the article there is the comment:

"Every static variable is stored on the heap, regardless of whether it's
declared within a reference type or a value type. There is only one slot in
total no matter how many instances are created. (There don't need to be any
instances created for that one slot to exist though.) Note that this heap is
separate from the normal garbage collected heap - it's known as a "high
frequency heap", and there's one per application domain. "

However, from reading this article:
http://msdn.microsoft.com/msdnmag/is...5/JITCompiler/

It makes the comment that only primitive static types will be stored on the
high frequency heap, whereas static value types and reference types are still
stored on the normal GC Heap.
Ooh, interesting. I'll look into it - thanks very much. The stuff about
the high frequency heap was going on what Willy has said in the past :)

I suspect that it's better for the sake of simplicity to remove the
reference to the high frequency heap and just refer to that article...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '07 #8
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Mark R. Dawson <Ma*********@di scussions.micro soft.comwrote:
>Hi Jon,
in the article there is the comment:

"Every static variable is stored on the heap, regardless of whether it's
declared within a reference type or a value type. There is only one slot in
total no matter how many instances are created. (There don't need to be any
instances created for that one slot to exist though.) Note that this heap is
separate from the normal garbage collected heap - it's known as a "high
frequency heap", and there's one per application domain. "

However, from reading this article:
http://msdn.microsoft.com/msdnmag/is...5/JITCompiler/

It makes the comment that only primitive static types will be stored on the
high frequency heap, whereas static value types and reference types are still
stored on the normal GC Heap.

Ooh, interesting. I'll look into it - thanks very much. The stuff about
the high frequency heap was going on what Willy has said in the past :)

I suspect that it's better for the sake of simplicity to remove the
reference to the high frequency heap and just refer to that article...

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


Jon, the article is wrong.
static fields whatever their type are always stored on the HighFrequencyHe ap, they can never
be members of object instances , so they are NEVER on the GC heap.

Consider following sample and my anotations.
....
class C
{
public int sc = 0;
public static S s;
}
public struct S
{
public int w;
public static long l;
}
class Program
{
static int v = 123;
static void Main()

{
S s1 = new S(); [1]
v = 128; [2]
C c1 = new C(); [3]
C.s = new S(); [4]
C c2 = new C(); [5]
S.l = 0xffffffffff; [6]
C.s.w = 255; [7]
s1.w = 123; [8]
}
}

[1] allocates s1 on the stack. But finally what get's on the stack is only the contents of
S.w, S.l ends on the (HighFrequencyH eap) HFH, and stays there until the AD unloads, s1 stays
on the stack until the stack unwinds.

[2] That's an easy one, v is on the HFH, and is assigned the value 128.

[3] c1 is a reference to a type C on the GC heap. It's 's' variable however, gets stored on
the HFH, and stay's there until the AD unloads. Note, it's size is 12 bytes only, that is
the header+sizeof(i nt), assuming X86 here. the instance gets freed when GC'd.

[4] Now it gets really complicated, the variable C.s is static, so, it's on the HFH, the
value pointed to by C.s gets stored on the GC heap, but there you'll find only the field
S.w, the static field S.l gets stored on the HFH.

[5] Here I create a new instance of C. Here c2 points to an instance of C on the GC heap.
But, it's "s" field is on the HFH, actually it's the same field as c1.S (that is same
address, so same value).

[6] assign a large value to S.l (on the HFH see 1).

[7] C.s.w is assigned a value 255, this value is on the GC heap see [4]

[8] s1.w is assigned the value 123, this value ends on the stack (see 1)

You can inspect this all, by using a native debugger like Windbg.exe, take care however when
using the SOS .DLL extension, this one gives you the wrong impression that static's are
instance members and stored in-line on the GC heap, but if you watch carefully you'll see
that effectively the static's are stored elsewhere (this is quite obvious isn't it, how
could they otherwise survive a GC sweep?).

To illustrate this last point, consider the debugger SOS output...

0:000!do 029b19cc
Name: Willys.C
MethodTable: 00103198
EEClass: 00101844
Size: 12(0xc) bytes
(C:\Develop\net fx\Junk\objlayo ut.exe)
Fields:
MT Field Offset Type VT Attr Value Name
790ff7f0 4000001 4 System.Int32 0 instance 0 sc
00103124 4000002 4 Willys.S 1 static 029b19a4 s
0:000dd 029b19cc
029b19cc 00103198 00000000 00000000 00103198

!do dumps the object at address 029b19cc (the GC heap), the output makes you believe that
the static field is part of the instance, well it's NOT and I think the author of the
article in MSDN Mag made the mistake in believing it was.
Note the size (12 bytes), that includes the sync#, MT pointer and an instance field 'sc'.
Note also the next field, see it's offset.... and it's attribute (static) so you see it's
not part of the instance...
notice also the fields value (029b19a4 ) again this is a pointer to the GC heap..
the "dd" command dumps the real contents of the address (029b19cc ), which is the real
object contents (less the sync# field), the first DWORD (00103198 ) is the MT pointer and
the next DWORD (00000000) is the value of sc, and that's it.

Willy.

Jan 4 '07 #9
Jon, the article is wrong.
static fields whatever their type are always stored on the
HighFrequencyHe ap, they can never be members of object instances , so they
are NEVER on the GC heap.
I don't know about C#, but in VB.NET you can call a "shared" member
(VB.NET's version of C#'s "static") on an instance as well as the type.
Jan 4 '07 #10

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

Similar topics

2
21634
by: Petra Neumann | last post by:
Hello, I just came across some sample code where a variable has been defined as: static public final int TABLE = 1, LIST = 2; I was wondering if there is a difference to writing public static final int TABLE = 1,...? Cheers, Petra
8
8795
by: Aman | last post by:
does it matter if a typedef is declared under public or private ? does any one (public or private ) constitute better design ? -- Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
6
8315
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream fStream=null; lock(fStream) //just in case we use it for multithreading to be thread safe {
5
6812
by: rettigcd | last post by:
I have several classes that all have the same static member: class A{ public static string Table = "TableA"; } class B{ public static string Table = "TableB"; }
2
1426
by: Jason Huang | last post by:
Hi, What's the difference "public static" and "static"? Do we have "protected static" and "private static"? If we do, in what situation will we apply them? Thanks for help. Jason
4
6277
by: sviau | last post by:
should cache-control be set to public or private on dynamic site. our content doesnt change for 24hrs, but because results from search pages vary so much, we cant cache the pages themselves. what other header info should i explicetly setting in the asp.net page to make sure that the site is cached as much as possible? Show Headers from http://www.mls.ca Request headers sent:
4
5797
by: rognon | last post by:
Hi there, I'm trying to do something, but I don't know if it's possible. Basically, I want to have a public static class method that could access a private object's method. I would like to be able to do : Class.method(InstanceOfClass); The method would then access a private function from Class by doing something like
6
3585
by: stoogots2 | last post by:
am using Visual Studio 2003, .Net Framework 1.1, C#. I get a SystemNullReferenceException when trying to do a hashtable.add(string,string) from a login page, and I do not understand why because all of the other data items from the Global.asax.cs are accessible in my pages. The key and value are not null (according to the watch window), so it is something else. This is likely something retarded, so I apologize in advance. It is my first...
4
15326
by: Ginney | last post by:
I'm taking an on-line course and They explain the difference between Private and Public. I understand the definition. Problem is, the only examples they give are Private and are used on Form Modules. Some examples don't even put Private or Public in front of "sub". Is there a particular module that you use public in? Is it not used on a Form module? Do you just go along writing Private Procedures and suddenly add a Public one? On to...
0
9791
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11138
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10412
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7970
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7123
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5990
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.