Share via


Does a private static variable make any sense?

Question

Wednesday, February 15, 2006 12:52 AM

Since static variables are set before a class instance is created, will a private static variable have the same scope as a public static variable? I am under the impression that static variables are available globally, so private static variable is same as public static variable.

All replies (3)

Wednesday, February 15, 2006 1:36 AM

If it is private static, it will only be available in the class it is declared in.


Wednesday, February 15, 2006 1:44 AM

Will the single private static variable be available to all instances of the class in which this variable is declared or each instance will have a separate private static variable copy?


Wednesday, February 15, 2006 2:51 AM

Static always means a single copy shared across all instances. Private is just an access qualifier, it means that shared member will not be available to derived classes. Somewhat unusual, but technically possible. Counter of instances of a class is one example.