← Back to C# Course | Chapter 6: OOP Basics | Lesson 6 of 7

Static Members

In this page:

  1. Static Members

Static Members

static fields and methods belong to the class itself, not to any individual object, and are shared across every instance. You call them through the class name rather than an object reference. Static members are useful for counters, utility methods, and shared configuration.

Note: A static field is a good way to count how many instances of a class have been created.

Example: Static Members

markup
using System;

class Counter
{
    public static int InstanceCount = 0;

    public Counter()
    {
        InstanceCount++;
    }

    public static int Double(int n) => n * 2;
}

class Program
{
    static void Main(string[] args)
    {
        new Counter();
        new Counter();
        new Counter();

        Console.WriteLine(Counter.InstanceCount);
        Console.WriteLine(Counter.Double(21));
    }
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.