public static and static

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

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
 
Jason.... If you do not explicitly declare the access modifier, then the
default
accessibility for class and struct members is private. private static
members are
useful for thread safe identifiers.

// static stuff
private static int uniqueID= 0;
private static int GetUniqueID()
{
lock(typeof(TestStatic))
{
return uniqueID++; // returns zero at start
}
}

Alternatively you can lock on a syncLock object.

Regards,
Jeff
 
See inline
Jason Huang said:
Hi,

What's the difference "public static" and "static"?
Do we have "protected static" and "private static"?

access modifiers (public, protects ...) and the static modifier are
independent.
You can combine static with any valid combinition of access modifiers.
Since private is default (where applicable), "private static" is the same as
"static".
If we do, in what situation will we apply them?

Simply, if a member should be private resp. protected and also static.
If you don't find a situation, don't use them ;-)
 
Back
Top