Dart static Keyword
The class variable and method are declared using the static keyword. In most cases, it controls the global data variable’s memory. Static variables and methods belong to the class rather than being specific instances. We can access a data member without constructing an object if we declare it as static since the static variable and methods are the same for all instances of the class. Static variables and methods can be accessed without the class object by placing the class name before the static variable or method. We can call the class method from the other classes by using the class name.
Dart Static Variable
The Dart static keyword refers to a variable that is declared inside of a class using the static keyword. Rather than being a particular instance, these are the members of the class. All instances of the class share a single copy of the static variable since all instances of the class handle the static variables in the same way. Memory is allocated only once during class loading and is used continuously by the program.
Point to Remember –
- Another way to identify the static variable is as a class variable.
- Each instance of a class shares a single copy of the static variable.
- The class name can be used to access it. We do not have to make an instance of that class that they are members of.
- The static methods provide immediate access to the static variables.
Declaring Static Variable
To declare a static variable, use the static keyword that Dart provides. The static keyword and the variable name are used to declare it. Below is the syntax.
Syntax
static [data_type] [variable_name];
Accessing Static Variable
Rather of making an object of the static variable, we can access it by using the class name itself. Below is the syntax.
Syntax
ClassName.staticVariableName;
Dart Static Method
Static variables and the static method have a similar notion. Rather of using a class object, the static methods are members of the class. The class’s static method may be called by the static methods, which are limited to using static variables. To access a class, we don’t need to make an instance of it. When we wish to use the static method in other classes, it comes in handy.
Points to Remember
- Rather of being its object, the static methods are the member class.
- Class methods are another name for static methods.
- The class name allows us to access static methods.
- Every instance of a class receives a specific copy of the static method.
Declaring Static Methods
The static keyword, the method name, and the return type can be used to declare a static method. Below is the syntax.
Syntax
static return_type method_name() {
//statement(s)
}
Calling Static Method
Rather than constructing an object, the static methods can be invoked simply passing in the class name to which they belong.
Syntax
className.staticMethod();
Example
class Student {
static String stdBranch; // Declaring static variable
String stdName;
int roll_num;
showStdInfo() {
print("Student's name is: ${empName}");
print("Student's salary is: ${roll_num}");
print("Student's branch name is: ${stdBranch}");
}
}
void main() {
Student std1 = new Student(); // Creating instances of student class
Student std2 = new Student();
// Assigning value of static variable using class name
Student.stdBranch = "Computer Science";
std1.stdName = "Ben Cutting";
std1.roll_num = 90013
std1.showStdInfo();
std2.stdName = "Peter Handscomb";
std2.roll_num = 90014
std2.showStdInfo();
}
Output
Student's name is: Ben Cutting
Student's salary is: 90013
Student's branch name is: Computer Science
Student's name is: Peter Handscomb
Student's salary is: 90014
Student's branch name is: Computer Science
Explanation
The class named Student is defined in the code above. It contains three fields, one function called showStdInfo(), and one static variable called stdBranch. We set values for the class variables and generated two instances of the Student class.
The class name and allocated value are used to access the static variable stdBranch. Next, we used the objects std1 and stu2 to call the showStdInfo() function. The student’s information was printed as an output.