Dart Static Members
Static Members in Dart
Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :
- All the objects of the class having static members share a single copy of the members, irrespective of how many objects have been created. Only a single copy of that member is created and shared among all the objects.
- It is always defined or initialized outside all the classes and functions using the scope resolution operator '::'. However, its declaration is done inside the class.
- It is visible only within the class it is declared in, but its lifetime is the entire program.
- We need not create an instance of the class to call the static members of a class, they can be called directly with the class name.
The 'static' Keyword
To declare members of the class as static, we use the 'static' keyword. The memory allocation of static members is similar to the global data variables. The static variables and methods are not an individual instance of the class but the member of the class. Since a single copy of the static member is maintained, it is the same for every instance of the class. Therefore, static members can be called without creating an object of the class. We can access it by putting the class name before the static variable or method. We can call the class method from the other classes using the class name.
Points to Remember -
- The static variable is also identified as a class variable.
- A single copy of the static variable is shared among the instance of a class.
- It can be accessed using the class name. We don't need to create an object of that class they belong to.
- The static variables can be accessed directly in the static methods.
Declaring Static Variable
Dart provides the static Keyword to declare the static variable. It is declared by using the static keyword followed by the variable name. The syntax is given below.
Syntax:
static [ data_type ] [ variable_name ] ;
Accessing Static Variable
We can access the static variable by using the class name itself instead of creating an object. The syntax is given below.
Syntax:
ClassName.staticVariableName;
Declaring Static Methods
We can declare the static method using the static keyword followed by the method name with the return type. The syntax is given below.
Syntax:
static return_type method_name( ) {
//statement( s )
}
Calling Static Method
The static methods can be called by using the class name, which they belong to instead of creating an object.
Syntax:
className.staticMethod();
Let's understand the following example.
Example -
class Student {
static String course = 'null'; // Declaring static variable
String name = 'null';
int enrol = 0;
display() {
print("Student's name is: ${name}");
print("Student's salary is: ${enrol}");
print("Student's branch name is: ${course}");
}
}
void main() {
Student s1 = new Student(); // Creating instances of student class
Student s2 = new Student();
// Assigning value of static variable using class name
Student.course = "BCA";
s1.name = "Rahul Rai";
s1.enrol = 155;
s1.display();
s2.name = "Rohit Malhotra";
s2.enrol = 157;
s2.display();
}
Output
Student's name is: Rahul Rai
Student's salary is: 155
Student's branch name is: BCA
Student's name is: Rohit Malhotra
Student's salary is: 157
Student's branch name is: BCA
Explanation:
In the above code, we declared the class called Student, which has three fields, including static variable course and one method display(). We created two instances of class Student and assigned values to the class variables.
The static variable course is accessed using the class name and assigned value since it is a static member. Then, we called the display() function by objects s1 and s2. It printed details of the student as an output.