Dart static Keyword

 

Dart static Keyword

The static keyword is used to declare the class variable and method. It generally manages the memory for the global data variable. The static variables and methods are the member of the class instead of an individual instance. The static variable or methods are the same for every instance of the class, so if we declare the data member as static then we can access it without creating an object. The class object is not required to access the static method or variable we can access it by putting the class name before the static variable or method. Using the class name, we can call the class method from the other classes.

Dart Static Variable

A variable which is declared using the static keyword inside the class is called Dart static keyword. These are the member of the class instead of a specific instance. The static variables are treated the same for all instances of the class; it means a single copy of the static variable is shared among all instances of classes. It allocates memory once and at the class loading and uses throughout the program.

Point to Remember -

  • The static variable is also identified as a class variable.
  • 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:

  1. 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 of it. The syntax is given below.

Syntax:

  1. ClassName.staticVariableName;  

Dart Static Method

The concept of the static method is also similar to static variable. The static methods are the member of the class instead of the class instance. The static methods can use only static variables and can invoke the static method of the class. We don't need to create an instance of class the access it. The static method is useful when we want to use it in other classes.

Points to Remember

  • The static methods are the member class instead of its object.
  • Static methods are also identifies as class methods.
  • We can access static methods using the class name.
  • A particular copy of the static method is distributed among all the instances of a class.

Declaring Static Methods

We can declare the static method by using the static keyword followed by the method name with the return type. The syntax is given below.

Syntax:

  1. static return_type method_name() {  
  2.  //statement(s)  
  3. }  

Calling Static Method

The static methods can be called by using the class name, which they belong to instead of creating an object.

Syntax:

  1. className.staticMethod();  

Let's understand the following example.

Example -

  1. class Student {  
  2.    static String stdBranch;  // Declaring static variable  
  3.    String stdName;  
  4.    int roll_num;  
  5.      
  6.    showStdInfo() {  
  7.      print("Student's name is: ${empName}");  
  8.      print("Student's salary is: ${roll_num}");  
  9.      print("Student's branch name is: ${stdBranch}");  
  10.   
  11.       }  
  12. }  
  13.   
  14. void main() {  
  15.    
  16.   Student std1 = new Student();  // Creating instances of student class   
  17.   Student std2 = new Student();  
  18.   // Assigning value of static variable using class name   
  19.   Student.stdBranch = "Computer Science";  
  20.     
  21.   std1.stdName = "Ben Cutting";  
  22.   std1.roll_num = 90013  
  23.   std1.showStdInfo();  
  24.   
  25.   std2.stdName = "Peter Handscomb";  
  26.   std2.roll_num = 90014  
  27.   std2.showStdInfo();  
  28. }  

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:

In the above code, we declared the class called Student, which has three fields including static variable stdBranch and one method showStdInfo(). We created two instances of class Student and assigned values to the class variables.

The static variable stdBranch accessed by using the class name and assigned value. Then, we called the showStdInfo() function by objects std1 and stu2. It printed details of the student as an output

Comments