Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to prove that only one instance of the object is created for static class?
In C#, a static class ensures that only one instance exists throughout the application's lifetime. To prove this concept, we can demonstrate how static variables maintain their state across multiple accesses, and how the static constructor is called only once.
A static class cannot be instantiated using the new keyword. Instead, all members belong to the type itself rather than to any specific instance. This behavior proves that only one "instance" of the static class exists in memory.
Syntax
Following is the syntax for declaring a static class with static members −
static class ClassName {
public static int staticVariable;
static ClassName() {
// static constructor - called only once
}
public static void StaticMethod() {
// static method
}
}
Using Static Constructor to Prove Single Instance
The static constructor is called only once when the class is first accessed, proving that only one instance exists −
using System;
static class Demo {
public static int count;
static Demo() {
Console.WriteLine("Static Constructor called");
}
}
class Program {
static void Main() {
Demo.count++;
Demo.count++;
Console.WriteLine("Count: " + Demo.count);
// Access static class again
Demo.count++;
Console.WriteLine("Count: " + Demo.count);
}
}
The output of the above code is −
Static Constructor called Count: 2 Count: 3
Notice that the static constructor is called only once, even though we accessed the static class multiple times. This proves that only one instance exists.
Using Static Variables Across Multiple Methods
Static variables maintain their state across different method calls, further proving the single instance concept −
using System;
static class Counter {
public static int value = 0;
static Counter() {
Console.WriteLine("Counter static constructor called");
}
public static void Increment() {
value++;
Console.WriteLine("Counter incremented to: " + value);
}
public static void Reset() {
value = 0;
Console.WriteLine("Counter reset to: " + value);
}
}
class Program {
static void Main() {
Counter.Increment();
Counter.Increment();
Counter.Reset();
Counter.Increment();
}
}
The output of the above code is −
Counter static constructor called Counter incremented to: 1 Counter incremented to: 2 Counter reset to: 0 Counter incremented to: 1
Demonstrating Memory Efficiency
Static classes demonstrate memory efficiency by maintaining state without creating multiple instances −
using System;
static class Logger {
public static int logCount = 0;
static Logger() {
Console.WriteLine("Logger initialized - this prints only once");
}
public static void Log(string message) {
logCount++;
Console.WriteLine($"Log #{logCount}: {message}");
}
}
class Program {
static void Main() {
Logger.Log("First message");
Logger.Log("Second message");
Logger.Log("Third message");
Console.WriteLine("Total logs created: " + Logger.logCount);
}
}
The output of the above code is −
Logger initialized - this prints only once Log #1: First message Log #2: Second message Log #3: Third message Total logs created: 3
Key Characteristics
-
Static constructor executes only once during the application's lifetime.
-
Static variables maintain their state across all accesses.
-
No
newkeyword can be used to create instances of static classes. -
All members must be declared as
static. -
Static classes are sealed by default and cannot be inherited.
Conclusion
Static classes in C# prove single instance behavior through their static constructor being called only once and static variables maintaining state across all accesses. This design pattern ensures memory efficiency and provides a reliable way to implement singleton-like behavior at the type level.
