What does the 'static' keyword do in a class?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

The static keyword in a class modifies the behavior of fields, methods, properties, constructors, or even nested classes, making them class-level members rather than instance-level members. Here's a breakdown of what it does in various contexts:

Key Behaviors of the static Keyword

1. Static Fields

A static field is shared among all instances of the class. It belongs to the class itself rather than any specific object. Changing its value in one instance reflects across all instances.

Example:

class Counter { public static int count = 0; // Static field public Counter() { count++; // Increment shared count } } Counter c1 = new Counter(); Counter c2 = new Counter(); Console.WriteLine(Counter.count); // Output: 2

Use Case: Use static fields for shared data or constants.

2. Static Methods

A static method is a function that can be called on the class itself, without requiring an instance of the class. Static methods cannot access non-static members directly.

Example:

class MathUtils { public static int Add(int a, int b) { return a + b; } } int result = MathUtils.Add(3, 5); // Output: 8

Key Rules:

  • Static methods can only access other static members of the class.
  • They cannot use the this keyword because they are not tied to an instance.

3. Static Properties

A static property provides a way to expose static fields or compute static values.

Example:

class Configuration { private static string _appName = "MyApp"; public static string AppName { get { return _appName; } set { _appName = value; } } } Console.WriteLine(Configuration.AppName); // Output: MyApp Configuration.AppName = "YourApp"; Console.WriteLine(Configuration.AppName); // Output: YourApp

4. Static Constructors

A static constructor is called automatically once, when the class is first accessed (either when a static member is accessed or when an instance of the class is created). It is used to initialize static fields.

Example:

class Logger { public static string logFilePath; static Logger() { // Static constructor logFilePath = "log.txt"; } } Console.WriteLine(Logger.logFilePath); // Output: log.txt

Key Rules:

  • A static constructor cannot have parameters.
  • It is executed only once per class, even if the class is accessed multiple times.

5. Static Classes

A static class can only contain static members. It cannot be instantiated, inherited, or contain instance members.

Example:

static class Utils { public static void PrintHello() { Console.WriteLine("Hello!"); } } Utils.PrintHello(); // Output: Hello!

Use Case: Use static classes for utility or helper methods that do not depend on instance data.

6. Static Nested Classes

A static nested class is a class defined inside another class with the static modifier. It can only access the static members of the enclosing class.

Example:

class OuterClass { public static string StaticData = "Outer Data"; public static class NestedClass { public static string GetData() { return StaticData; // Access static data of OuterClass } } } Console.WriteLine(OuterClass.NestedClass.GetData()); // Output: Outer Data

Summary of static Keyword

ContextBehavior
Static FieldsShared among all instances, belongs to the class.
Static MethodsBelongs to the class, callable without an instance, only accesses static members.
Static PropertiesProvides controlled access to static fields.
Static ConstructorsAutomatically initializes static members, runs once when the class is first accessed.
Static ClassesContains only static members, cannot be instantiated or inherited.
Static Nested ClassesCan access only static members of the enclosing class.

The static keyword provides powerful capabilities to define class-level behavior, ensuring members are shared or accessible independently of any specific instance. For deeper exploration of design patterns and efficient programming, check out Grokking Advanced Coding Patterns for Interviews or Grokking System Design Fundamentals on DesignGurus.io!

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Enhancing diagramming skills for system design visualization
What are Microsoft's benefits?
How long will I hear after an interview?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.