Skip to main content
A static method is a function declared within a class using the static modifier, associating it directly with the class namespace rather than a specific instance. These methods execute without an object context, do not have access to the this keyword, and are resolved at compile-time.

Declaration Syntax

To define a static function, prepend the static keyword to the method declaration within the class body.
class DataHandler {
  static const int threshold = 100;

  // Static method declaration
  static bool isValid(int value) {
    // Can access other static members
    return value <= threshold;
  }
}

Invocation

Static methods are invoked directly on the class identifier. They are not accessible through an instance of the class.
void main() {
  // Correct invocation via Class identifier
  bool result = DataHandler.isValid(50);

  // Incorrect invocation (Compilation Error)
  // var handler = DataHandler();
  // handler.isValid(50); 
}

Scope and Access Rules

Static methods operate under specific scope constraints regarding class members:
  1. No this Context: Static methods do not have access to the this keyword because they are not attached to an instance.
  2. Instance Member Access: They cannot access instance variables or instance methods directly. Attempting to reference a non-static member results in a compilation error.
  3. Static Member Access: They can access other static variables and static methods within the same class without an explicit class prefix.
class ScopeExample {
  int instanceCount = 0;
  static int staticCount = 0;

  static void staticMethod() {
    staticCount++;      // Allowed: Accessing static member
    // instanceCount++; // Error: Instance members are inaccessible
    // this.toString(); // Error: 'this' is undefined in static context
  }

  void instanceMethod() {
    staticCount++;      // Allowed: Instance methods can access static members
    staticMethod();     // Allowed
  }
}

Inheritance and Polymorphism

Static methods in Dart are not inherited. Unlike instance methods, static methods belong strictly to the class in which they are defined.
  • No Overriding: Since static methods are not inherited, they cannot be overridden using the @override annotation.
  • No Dynamic Dispatch: Calls to static methods are statically dispatched based on the class specified at the call site.
  • Namespace Isolation: If a subclass defines a static method with the same name as a superclass, the two methods are entirely unrelated. The subclass method does not shadow or hide the superclass method because the superclass method was never accessible via the subclass namespace.
class Base {
  static void identify() => print("Base");
}

class Derived extends Base {
  // This is a distinct method, unrelated to Base.identify
  static void identify() => print("Derived");
}

class EmptyDerived extends Base {
  // No static methods defined
}

void main() {
  Base.identify();         // Output: Base
  Derived.identify();      // Output: Derived
  
  // EmptyDerived.identify(); // Compilation Error: The method 'identify' isn't defined for the class 'EmptyDerived'.
}

Function Tear-offs

Static methods are first-class objects and can be treated as function values. They can be assigned to variables or passed as arguments without immediate execution.
class Calculator {
  static int add(int a, int b) => a + b;
}

void main() {
  // Tear-off: Assigning the static function to a variable
  int Function(int, int) operation = Calculator.add;
  
  print(operation(10, 20)); // Output: 30
}
Master Dart with Deep Grasping Methodology!Learn More