The static keyword is used to define members that belong to a class or structure rather than to a specific object. Static members are shared across all instances of a class and can be accessed using the class name without creating an object.
- Static members are associated with the type and are shared among all instances of the class.
- Static methods cannot directly access instance members or use the this keyword.
Example: Program to demonstrates how a static variable is shared between multiple Student objects.
using System;
class Student
{
int rollNo;
string name;
// Static variable
public static string TrainingCenter = "GFG";
public Student(int r, string n)
{
rollNo = r;
name = n;
}
public void Display()
{
Console.WriteLine(rollNo + " " + name + " " + TrainingCenter);
}
}
class Program
{
static void Main()
{
Student s1 = new Student(101, "Ravi");
Student s2 = new Student(102, "Amit");
s1.Display();
s2.Display();
}
}
Output
101 Ravi GFG 102 Amit GFG
Explanation: The TrainingCenter variable is static, so both Student objects share the same value.
Syntax
static dataType memberName;
For a static method:
static returnType MethodName()
{
// Method body
}
Types of Static Members
The static keyword can be used with several class members in C#, including static fields, static constructors, static methods, and static nested classes.
1. Static Variables (Static Fields)
A static field belongs to the type rather than to individual objects. All instances of the class share the same static field.
using System;
class Geeks
{
public static int a = 10;
static void Main()
{
Console.WriteLine("Value of a: " + Geeks.a);
}
}
Output
Value of a: 10
Explanation: The static field a is accessed directly using the class name Geeks, without creating an object.
2. Static Constructor
A static constructor initializes static data or performs initialization tasks for a class. It is called automatically by the runtime before the class is first used, subject to C# initialization rules.
using System;
class Geeks
{
static int a;
static int b;
static Geeks()
{
Console.WriteLine("Static constructor executed.");
a = 10;
b = a * 4;
}
static void Main()
{
Console.WriteLine("From Main");
Console.WriteLine("Value of a: " + a);
Console.WriteLine("Value of b: " + b);
}
}
Output
Static constructor executed. From Main Value of a: 10 Value of b: 40
Explanation: The static constructor initializes a and b before Main() accesses them. A static constructor has no access modifier and takes no parameters.
3. Static Methods
A static method belongs to the class rather than to an individual object. It can be called using the class name without creating an instance.
- Can directly access other static members.
- Cannot directly access instance fields or instance methods.
- Cannot use the this keyword.
using System;
class Geeks
{
static int a = GetValue();
static Geeks()
{
Console.WriteLine("Inside static constructor");
}
static int GetValue()
{
Console.WriteLine("From GetValue");
return 20;
}
static void Main()
{
Console.WriteLine("Value of a: " + a);
Console.WriteLine("From Main");
}
}
Output
From GetValue Inside static constructor Value of a: 20 From Main
Explanation: The static field initializer calls GetValue(), which returns 20. The static constructor then executes before Main().
4. Static Nested Classes
A class declared inside another class can be marked static. A static nested class cannot be instantiated and can contain only static members. It can be accessed using the outer class name.
using System;
class Outer
{
public static class Inner
{
public static void Show()
{
Console.WriteLine("Static Nested Class Method");
}
}
static void Main()
{
Outer.Inner.Show();
}
}
Output
Static Nested Class Method
Explanation: The nested class Inner and its static method Show() are accessed using Outer.Inner.Show() without creating an object.
Note: C# does not support static initialization blocks. It uses a static constructor for type initialization. Also, a static class cannot be instantiated and can contain only static members.
Static Vs Non-Static
The following table highlights the differences between static and non-static members.
Aspect | Static | Non-Static |
|---|---|---|
Belongs to | Class or structure | Individual object |
Memory and state | Shared at the type level | Separate instance state for each object |
Access | Usually through the type name | Through an object reference |
Direct access | Can directly access static members | Can access both static and instance members, subject to access rules |
Common use | Utility methods and shared data | Object-specific data and behavior |