LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 2812 min read

Static Members

Learn static fields, methods, and classes, and how they differ from instance members.

On this page 6 sections

Instance vs Static

Instance members belong to a specific object; every object gets its own copy. Static members belong to the class itself, shared across every instance.

Static Fields

class Counter
{
public static int Count = 0;
public Counter()
{
Count++; // shared across every instance
}
}
new Counter();
new Counter();
new Counter();
Console.WriteLine(Counter.Count);
Output

Click Run to see what this code prints.

Static Methods

Static methods belong to the class, not an object, and can only directly access other static members. Math.Sqrt() and Console.WriteLine() are both static methods.

class MathHelper
{
public static int Square(int n) => n * n;
}
Console.WriteLine(MathHelper.Square(5)); // no object needed

Static Classes

A class marked static can contain only static members and can never be instantiated — used for pure utility classes.

static class Constants
{
public const double TaxRate = 0.08;
}
Console.WriteLine(Constants.TaxRate);

FAQs

No — a static method has no associated object, so it can only access other static members directly.

Summary

  • Static members belong to the class, shared across all instances.
  • Static methods are called on the class itself, not an object.
  • Static classes are used for pure utility functions and can't be instantiated.
Next Lesson →

Properties