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:
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.
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:
Explanation:
Instance Members:
brand
andyear
are instance variables. They hold values specific to each object. Forcar1
andcar2
, thebrand
andyear
values are different because they represent different cars.displayInfo()
is an instance method that can be called on each instance of theCar
class. It prints the details of the car.
Static Members:
count
is a static variable. It keeps track of the number ofCar
objects created. This variable is shared across all instances of the class. Whenever a new car is created, thecount
is incremented.displayCount()
is a static method that can be accessed directly using the class name. It prints the total number ofCar
objects created. You don’t need to create an instance of the class to call it.
Accessing Static Members:
- Static members (like
count
anddisplayCount()
) 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.
- Static members (like
Output:
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?
- Memory efficiency: Static members are shared among all objects, reducing memory usage since there's only one copy of the static variables.
- 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).
- 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.