Static Classes and Static Class Members


A static class is a class which can not be instantiated. We do not need to create an object of a static class like we did in non-static classes. Members of static classes can be accessed directly using the class name followed by a (.) and class member name. Class Members can be methods, fields, properties, or events. A static class can contain only the static members while a non-static class can contain static members. Also, Static classes are by-default sealed so they cannot be inherited.

To declare a class and its members as static, 'static' keyword are used as shown below,
  1. using System;
  2. namespace Tutpoint
  3. {
  4. // Static class
  5. static class TutLovers
  6. {
  7. // Static Variable
  8. public static int val_Static;
  9. // Static Method
  10. public static void Addition_Static()
  11. {
  12. Console.Write("Static Method");
  13. }
  14. // Static property
  15. public static int Addition_NonStatic
  16. {
  17. get; set;
  18. }
  19. }
For more understanding see the example shown below,

The example shown below contains two non-static classes named "TutLovers" and "TutHaters". Here a class "TutLovers" contains static as well as non-static methods and variable. When we access them from class "TutHaters" then, For the static members, they will be accessed directly using class name followed by (.) and
For non-static members, we have created an object of that class.
  1. using System;
  2. namespace Tutpoint
  3. {
  4. class TutLovers
  5. {
  6. public static int val_Static;
  7. public int val_NonStatic;
  8. public static void Addition_Static()
  9. {
  10. Console.Write("Static Method");
  11. }
  12. public void Addition_NonStatic()
  13. {
  14. Console.Write("Non-Static Method");
  15. }
  16. }
  17. //If the class is non-static
  18. class TutHaters
  19. {
  20. public void Haters()
  21. {
  22. //Object of TutLovers class is created
  23. TutLovers tutLovers = new TutLovers();
  24. //Non-Static members can be called only from object
  25. tutLovers.Addition_NonStatic();
  26. tutLovers.val_NonStatic = 5;
  27. //Static members can only be called using class_name followed by a (.)
  28. TutLovers.Addition_Static();
  29. TutLovers.val_Static = 5;
  30. /* When we try to access static member from object then it will create an error
  31. Error as "Members 'TutLovers.Addition_Static()' can not be accessed with an instance referen -ce; qualify it with a type name instead" */
  32. tutLovers.Addition_Static();
  33. /* When we try to access non-static member using class_name then it will create an error
  34. Error as "An object reference is required for the non-static field , method or property 'TutLovers.Addition_NonStatic()'" */
  35. TutLovers.Addition_NonStatic();
  36. }
  37. }
  38. }
As shown above, when we try to access static member with an instance reference, it will create an error as "Members 'TutLovers.Addition_Static()' can not be accessed with an instance reference; qualify it with a type name instead" and when we try to access non-static member using class_name then it will create an error as "An object reference is required for the non-static field, method or property 'TutLovers.Addition_NonStatic()'".

Note
  1. There exists only one copy for every static member regardless of the number of instances of the class.
  2. Static methods can access only static members. They cannot access non-static members.
  3. A static class can only contain static members.