Dart static keyword
Dart static keyword
The Dart static keyword is used to declare the class variables and methods. The static variables/methods belong to the class instead of individual objects.
Dart static keyword
When we declare a data member static, we can access it without creating a class object. The static variables/methods are the same for every class object. To access a static method or variable, we can put the class name before the static variable or method.
We use the static keyword to declare a variable or a method inside the class. A static variable is treated the same for all instances of the class; it means a single copy of the static variable is shared among all the instances of the class. The memory is allocated when the class is loaded and is used for the entire program’s life.
Declare static variable
A static variable is also known as the class variable. We can declare the static variable by using the static keyword followed by the data type and the variable name.
static data_type variable_name;
static data_type variable_name = <initialization> ;
Example:
static int number = 10;
Access static Variable
We can access the static variable by using the class name itself instead of creating an object of it.
The general syntax to access the static variable is as follows:
ClassName.staticVariable;
The static members are bound to the class itself and not to the instances of the class. Therefore, they are commonly used for constants or utility functions that are not related to the class objects.
—
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/
More information on Dart: