473,836 Members | 2,079 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
15 34619
Scott M. <s-***@nospam.nosp amwrote:
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.
Yes, but that doesn't change where it's stored.

--
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 4 '07 #11
Willy Denoyette [MVP] <wi************ *@telenet.bewro te:
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.
Well, looking at the article closely, I don't think it actually says
they can be on the normal GC heap. It just says that they can be in the
HandleTable instead of the MethodTable (with a reference from the
MethodTable). It doesn't go into details about the HandleTable,
unfortunately.

Of course, I may have misread the MSDN article - is there any specific
statement in it that you consider incorrect?

--
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 4 '07 #12
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** @msnews.microso ft.com...
Willy Denoyette [MVP] <wi************ *@telenet.bewro te:
>static fields whatever their type are always stored on the
HighFrequencyH eap, they can never
be members of object instances , so they are NEVER on the GC heap.

Well, looking at the article closely, I don't think it actually says
they can be on the normal GC heap. It just says that they can be in the
HandleTable instead of the MethodTable (with a reference from the
MethodTable). It doesn't go into details about the HandleTable,
unfortunately.

Of course, I may have misread the MSDN article - is there any specific
statement in it that you consider incorrect?

Actually no I don't, actually the article is right in mentioning the HANDLE table.
Unfortunately the article is based on the shared code CLI source, which is actually not what
the retail version of the CLR looks like. The GC and the JIT for instance are completely
different, but you are right, it doesn't say it's on the GC heap, while actually it is, be
it on the LOH and not on the Generational heap (gen0, 1 and 2).
Now, I've done some more research to find out where static fields, holding references to
boxed value types, actually have their roots.

Reconsider the previous posted sample:

<snip>
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();
v = 128;
C c = new C();
C.s = new S();
C c2 = new C();
S.l = 0xffffffffff;
C.s.w = 255;
s1.w = 123;
A ---->
</snip>

What we have at A is (I wish I had chosen some meaningful names):
1) v is on the HFH holding the value 128.
2) S.l is on the HFH holding the value 0xffffffff.
3) C.s is on the LOH pointing to S boxed on the GC heap, with C.s.w holding the value 255.
4) s1.w is on the stack holding the value 123.

What's important is 3), here the static field reference 's', pointing to the boxed instance
of S, is on the LOH.
Now, why is this on the LOH?
The (Pinned!) *Handle table* is actually an object[], and this one is allocated on the LOH
(at the very beginning) with an initial size of 1020 elements. My guess is that it is
initially created on the LOH and not on the generational heap, such that the GC (and the
JIT) doesn't need to track it's actual size and location and so, he can keep it "fixed"
wihout disturbing the GC.
Note that here the static 's' variable is just an entry in this array, which is actually the
root of the boxed S instance.

Conclusion: all static's are in the HFH unless they hold references to boxed value or
reference types, in which case they are in the handle table which is on the LOH. Note that
all this is not documented, so it's really an implementation detail that might change with
the CLR version.

Willy.
Jan 4 '07 #13
I didn't say that it did. I was responding to Willy's message about static
members not being members of an instance.
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** @msnews.microso ft.com...
Scott M. <s-***@nospam.nosp amwrote:
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.

Yes, but that doesn't change where it's stored.

--
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 4 '07 #14
Scott M. <s-***@nospam.nosp amwrote:
I didn't say that it did. I was responding to Willy's message about static
members not being members of an instance.
They're not members of an instance. Just because you can access them as
if they were doesn't make them members.

(FWIW, I think it's a pity that VB.NET *does* allow you to do that.
Java allows the same thing, and it's a potential source of bugs because
it looks like you're doing something instance-specific when you're
not.)

--
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 4 '07 #15
Static Data is a shared data
It is an Class level data
The public & Private It's simply a matter of accessibility.
Public static members can be accessable to any assembly where the
static member is declared. Private static members can only be
accessible
from within the class.

Jan 18 '07 #16

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
8791
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
8311
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
6807
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
6275
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
5795
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
3580
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
15318
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
9668
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
10844
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...
0
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10590
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
10254
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
7791
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
6979
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
5649
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...
3
3113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.