What are the advantages of non-static class over static class?
static class need not be instanciated. so we can directly use ClassName.MemberName
, so then whats the use of nonstatic class
What are the advantages of non-static class over static class?
static class need not be instanciated. so we can directly use ClassName.MemberName
, so then whats the use of nonstatic class
Think of a class as a building blueprint and instances of that class (objects) as buildings built according to the design in the blueprint.
Given one blueprint, you can create many buildings. Each of these buildings has the same structure, but each is independent - one could have its door open while another has its door closed.
A static class is like a blueprint that can never be used to build any houses. You can still do things like tear it up or spill coffee on it, but you could never open the door on the blueprint since it doesn't have an actual door, only the design for a door.
Another way of describing this situation is by considering 'state'. A building (object) has some state (e.g. whether the door is open or not) and the state of each building can be different (e.g. whether it's own door is open). A blueprint (class) can also have (static) state (e.g. whether coffee has been spilled on it or not) but this is different and separate to the state associated with the buildings built according to the blueprint.
In the example above, lines 1, 2, and 3 would not be possible if
House
was a static class. Note thatopenDoor
is invoked on an instance ofHouse
whilespillCoffee
is invoked on the class itself. Note also that at the point of line 4 the red house's door is open but the blue house's door is still closed.An advantage of non-static class is that static classes are horrible to unit test - they can't easily be mocked, and they don't support interfaces.
Say you need to store information and operations that encapsulate an employee.
Now, lets say your program needs to compare two Employees at the same time?
Exactly! With a non static employee class you can instantiate two (or more) instances of the Employee class, each of these objects represents a different Employee.
You could not do this of Employee was static.
e.g.
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself
A static class has only one instance of the class itself so you cannot create multiple instance of a static class.
looking at your question you cannot set two different values to the below property if the class is static because there will be only one instance of
ClassName
in memorymore info at
http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
A static class cannot be instanced(technically it is, but only once), and the static constructor is "usually" called on the first access to the class, which can be difficult to track and coordinate. Static classes are good if you want one class that just gathers some utility methods. Math in the .net framework is a good example. Also they are usefull for extension methods. Besides that i don't use them.
If you need a single instance, and don't want to be bound to the constraints a static class has, the Singleton pattern is very useful. A nice article about the difference between Singletons and static classes.
And of course, a non static class must be instanced, stored in a variable and can be instanced multiple times.
Example:
A static class, just helper methods for loading a file:
a Singleton that is the main point to create textures.
an actual texture, which i might have many of in my application.