Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a class is blueprint for objects, what about static class?

Reading C# Step by Step, the author mentiones the class is just blueprint for objects and itself is useless. Well, how then comes static classes can work alone?

I do understand the concept that static classes cannot be instantiated and exist as one unique instance with its static members. But that ruins, a bit, the metaphor of classes as blueprints. How could static classes be explained in relation to this blueprint idea?

like image 637
Ptr Avatar asked Dec 06 '22 01:12

Ptr


1 Answers

No, a static class doesn't exist as one unique instance. There are no instances of a static class. There are just static members, which are associated with the type itself, rather than any instance.

Once you get the idea of static meaning "related to the class, not an instance" it makes sense, IMO. It's hard to come up with particularly real-world analogies along the "blueprint" lines though.

It's worth noting that the concept of static methods is exactly same for "normal" classes as for static classes: even with a normal class, you can call a static method without ever creating an instance of the class.

like image 75
Jon Skeet Avatar answered Jan 28 '23 09:01

Jon Skeet