Dart Instance vs Static Members


Instance vs Static Members in Dart

In Dart, instance members and static members are two types of members that belong to different parts of a class:

  1. Instance Members: These members (fields, methods) belong to an instance (object) of the class. Each object created from the class has its own copy of instance members. They can be accessed via an object of the class.

  2. Static Members: These members belong to the class itself rather than to any specific instance of the class. They are shared across all instances of the class. Static members can be accessed directly using the class name, without creating an object of the class.

Key Differences between Instance and Static Members:

  • Instance Members:

    • Belong to the object instance.
    • Each object has its own copy of instance variables.
    • Can be accessed using the object reference.
    • Can access both static and instance members.
  • Static Members:

    • Belong to the class itself.
    • Only one copy of the static variable exists, shared by all instances.
    • Can be accessed using the class name.
    • Cannot access instance members directly (since they don't belong to any specific object).

Example of Instance vs Static Members in Dart

Let's look at an example to understand the differences:

class Car { // Instance members String brand; int year; // Static member static int count = 0; // Constructor Car(this.brand, this.year) { // Incrementing the count for each object created count++; } // Instance method void displayInfo() { print('Brand: $brand, Year: $year'); } // Static method static void displayCount() { print('Total cars created: $count'); } } void main() { // Creating instances of Car var car1 = Car('Toyota', 2020); var car2 = Car('Honda', 2021); // Accessing instance members using the object car1.displayInfo(); // Output: Brand: Toyota, Year: 2020 car2.displayInfo(); // Output: Brand: Honda, Year: 2021 // Accessing static members using the class name Car.displayCount(); // Output: Total cars created: 2 // Accessing static member via object (not recommended) print('Accessing static member via object: ${car1.count}'); // Output: 2 }

Explanation:

  1. Instance Members:

    • brand and year are instance variables. They hold values specific to each object. For car1 and car2, the brand and year values are different because they represent different cars.
    • displayInfo() is an instance method that can be called on each instance of the Car class. It prints the details of the car.
  2. Static Members:

    • count is a static variable. It keeps track of the number of Car objects created. This variable is shared across all instances of the class. Whenever a new car is created, the count is incremented.
    • displayCount() is a static method that can be accessed directly using the class name. It prints the total number of Car objects created. You don’t need to create an instance of the class to call it.
  3. Accessing Static Members:

    • Static members (like count and displayCount()) are accessed using the class name (Car.displayCount()). You can also access them through an instance, though this is not recommended because static members are associated with the class itself, not instances.
    • car1.count still works, but accessing static members this way can be confusing and is not considered best practice.

Output:

Brand: Toyota, Year: 2020 Brand: Honda, Year: 2021 Total cars created: 2 Accessing static member via object: 2

Key Points to Remember:

  • Instance members are tied to specific objects and can be accessed only through instances.
  • Static members are tied to the class itself, shared among all instances, and can be accessed directly via the class name.
  • You can use static methods to perform operations that don't depend on instance variables.
  • Static variables are shared across all instances of a class and maintain their values even when no object is created.

Why Use Static Members?

  1. Memory efficiency: Static members are shared among all objects, reducing memory usage since there's only one copy of the static variables.
  2. Common functionality: Static methods can be used for functionality that doesn't rely on individual instances of the class (e.g., counting objects, utility functions).
  3. Global state: Static variables can represent global state or configuration settings that need to be accessed across instances of the class.

By understanding the differences between instance and static members, you can better design classes and manage state within your Dart applications.