Static Members

Static members in Dart belong to the class itself rather than to any specific instance of the class. Think of them as shared resources that all instances of a class can access, like a communal coffee machine in an office that everyone uses!

What are Static Members?

Static members include:

  • Static variables (also called class variables)
  • Static methods (also called class methods)

These members are accessed using the class name directly, without creating an instance of the class.

Static Variables

Static variables are shared among all instances of a class. They're initialized only once when the class is first accessed.

class Counter {
  static int totalCount = 0;
  int instanceCount = 0;
  
  Counter() {
    totalCount++;
    instanceCount++;
  }
  
  void displayCounts() {
    print('Instance count: $instanceCount');
    print('Total count: $totalCount');
  }
}

void main() {
  var counter1 = Counter();
  var counter2 = Counter();
  var counter3 = Counter();
  
  counter1.displayCounts(); // Instance: 1, Total: 3
  counter2.displayCounts(); // Instance: 1, Total: 3
  counter3.displayCounts(); // Instance: 1, Total: 3
  
  // Accessing static variable directly
  print('Total objects created: ${Counter.totalCount}'); // 3
}

Static Methods

Static methods can be called without creating an instance of the class. They can only access static variables and other static methods.

class MathUtils {
  static const double pi = 3.14159;
  
  static double calculateCircleArea(double radius) {
    return pi * radius * radius;
  }
  
  static double calculateCircleCircumference(double radius) {
    return 2 * pi * radius;
  }
  
  static bool isPrime(int number) {
    if (number < 2) return false;
    for (int i = 2; i <= number ~/ 2; i++) {
      if (number % i == 0) return false;
    }
    return true;
  }
}

void main() {
  // Calling static methods without creating an instance
  double area = MathUtils.calculateCircleArea(5.0);
  double circumference = MathUtils.calculateCircleCircumference(5.0);
  bool isPrimeNumber = MathUtils.isPrime(17);
  
  print('Area: $area');
  print('Circumference: $circumference');
  print('Is 17 prime? $isPrimeNumber');
}

Key Characteristics

AspectStatic MembersInstance Members
AccessThrough class nameThrough object instance
MemoryShared among all instancesSeparate for each instance
InitializationOnce when class is first accessedEvery time an object is created
Can accessOnly static membersBoth static and instance members

Common Use Cases

  1. Utility Functions: Mathematical operations, string manipulations
  2. Constants: Shared values that don't change
  3. Counters: Tracking total instances or operations
  4. Factory Methods: Alternative constructors
class Student {
  static int totalStudents = 0;
  static const String schoolName = "Dart Academy";
  
  String name;
  int rollNumber;
  
  Student(this.name, this.rollNumber) {
    totalStudents++;
  }
  
  // Static method to get school info
  static String getSchoolInfo() {
    return 'Welcome to $schoolName! Total students: $totalStudents';
  }
  
  // Static factory method
  static Student createWithAutoRoll(String name) {
    return Student(name, totalStudents + 1);
  }
}

void main() {
  var student1 = Student("Alice", 101);
  var student2 = Student.createWithAutoRoll("Bob");
  
  print(Student.getSchoolInfo()); // Welcome to Dart Academy! Total students: 2
}

Important Notes

  • Static methods cannot access instance variables or methods directly
  • You cannot use this keyword in static methods
  • Static variables are lazy-initialized (created when first accessed)
  • Static members are great for utility functions that don't need object state

Static members are perfect when you need functionality that's related to a class but doesn't depend on any specific instance of that class!