C# Static Keyword: Mastering Static Variables, Methods, and Classes
C# Static Keyword
This guide explains the static keyword in C#, covering static variables, methods, and classes with clear, real-world examples.
In C#, applying the static keyword to a member creates a single shared instance for all objects of that type. Instead of each instance holding its own copy, the static member lives once per type.
Static Variables in C#
When a field is declared static, you can access it directly through the class name, without creating an object.
using System;
namespace StaticKeyword {
class Student {
// static variable shared by all students
public static string department = "Computer Science";
}
class Program {
static void Main(string[] args) {
// Access the static field via the class name
Console.WriteLine("Department: " + Student.department);
Console.ReadLine();
}
}
}
Output
Department: Computer Science
The field department is shared by every Student instance, so we reference it as Student.department.
Static Variables vs. Instance Variables
Instance variables belong to individual objects, each with its own copy. For example:
class Student {
public string studentName;
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
Student s2 = new Student();
}
}
Here, s1 and s2 have separate studentName fields. In contrast, a static field is common to all instances and can be accessed without creating an object.
Example: Static vs. Instance Variables
using System;
namespace StaticKeyword {
class Student {
public static string schoolName = "Programiz School"; // shared
public string studentName; // unique
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
s1.studentName = "Ram";
Console.WriteLine("Name: " + s1.studentName); // instance
Console.WriteLine("School: " + Student.schoolName); // static
Student s2 = new Student();
s2.studentName = "Shyam";
Console.WriteLine("Name: " + s2.studentName);
Console.WriteLine("School: " + Student.schoolName);
Console.ReadLine();
}
}
}
Output
Name: Ram School: Programiz School Name: Shyam School: Programiz School
The static field schoolName is identical for all students, making it memory‑efficient and logically consistent.
Static Methods in C#
Static methods are invoked on the class itself, not on an instance. They can only access other static members.
class Test {
public static void Display() {
// implementation
}
}
class Program {
static void Main(string[] args) {
Test.Display();
}
}
All objects of Test share the same Display method, so you call it via the class name.
Example: Static and Non-Static Methods
using System;
namespace StaticKeyword {
class Test {
public void Display1() {
Console.WriteLine("Non static method");
}
public static void Display2() {
Console.WriteLine("Static method");
}
}
class Program {
static void Main(string[] args) {
Test t1 = new Test();
t1.Display1(); // instance call
Test.Display2(); // static call
Console.ReadLine();
}
}
}
Output
Non static method Static method
Note: The Main method itself is static, allowing it to run without an instance.
Static Classes
A static class cannot be instantiated and can only contain static members. It is useful for grouping helper methods.
using System;
namespace StaticKeyword {
static class Test {
static int a = 5;
static void Display() {
Console.WriteLine("Static method");
}
static void Main(string[] args) {
// Test t1 = new Test(); // ❌ Compile error
Console.WriteLine(a);
Display();
}
}
}
Attempting to instantiate Test results in compile errors CS0723 and CS0712, confirming its static nature.
Static classes cannot be inherited. For example:
static class A { /* ... */ }
// class B : A { /* ... */ } // ❌ Error
Accessing Static Members Inside the Class
Within the same class, you can refer to static fields and methods directly, without the class qualifier.
using System;
namespace StaticKeyword {
class Test {
static int age = 25;
public static void Display() {
Console.WriteLine("Static method");
}
static void Main(string[] args) {
Console.WriteLine(age); // direct access
Display(); // direct access
Console.ReadLine();
}
}
}
Output
25 Static method
Here, age and Display are used without prefixing them with Test..
C Language
- Mastering C# Inheritance: Concepts, Types, and Practical Code
- Understanding C# Nested Classes: Definition, Usage, and Inheritance
- Master C++ Inheritance: Build Powerful Classes with Reusable Code
- Mastering C++ Class Templates: A Practical Guide
- Understanding Java’s final Keyword: Variables, Methods, and Classes
- Understanding Java Nested Static Classes: Usage, Differences, and Examples
- Understanding Storage Classes in C: auto, extern, static, register Explained
- Understanding C# Program Structure: Core Components & Example
- Mastering C# Inheritance: Build Reusable, Maintainable Code
- C# Polymorphism: Static vs Dynamic Binding Explained