Dart static Keyword

static is a keyword in dart language and refers to the current class.

static keywords apply to method or class instance variables.

Class is a blueprint for an object.

Instance variables have different copies of data for each object.

Suppose, you want to maintain a single copy of data for all instances, Then the data variable must be declared as static in a class. This is the same case with methods.

usually, instance variables and methods are called with object name

object.instancevariable
object.method()

Static variables and methods are called with class names directly

class.instancevariable
class.method()

Syntax:

this.instancevariable
this.method()

Dart static variable

variables are declared with the static keyword in the class for instance variables. static variables are also called class variables.

Syntax:

Here is a syntax for declaring a static variable in a class


static datatype variablename;  

Static variables are accessed with class name

classname.variablename

Here is an example for creating and accessing static variables in dart with example

These examples are two instance variables and one static variable.

Static variables are accessed using class name instance variables are accessed using the object of a class

class Employee {
  static String group = "sales";
  int id = 0;
  String name = '';
  Employee(int id, String name) {
    this.id = id;
    this.name = name;
  }
  printEmployee() {
    print("Name is: ${name}");
    print("Id is : ${id}");
    print("Group: ${group}");
  }
}

void main() {
  Employee e1 = new Employee(1, "john");
  Employee e2 = new Employee(2, "Andrew");

  e1.printEmployee();
  e2.printEmployee();
  print(Employee.group);
  //print(Employee.id);// compilation error
  // print(e1.group); // compilation error

  print(e1.id);
  print(e1.group);
}

Output:

Name is: john
Id is: 1
Group: sales
Name is: Andrew
Id is : 2
Group: sales

Dart static methods

static keywords also apply to methods in a dart class.

These methods also called class methods, called using a class

class Employee {
  static String group = "sales";
  int id = 0;
  String name = '';
  Employee(int id, String name) {
    this.id = id;
    this.name = name;
  }
  printEmployee() {
    print("Name is: ${name}");
    print("Id is : ${id}");
  }

  static printEmployeeGroup() {
    print("Group: ${group}");
  }
}

void main() {
  Employee e1 = new Employee(1, "john");
  Employee e2 = new Employee(2, "Andrew");

  e1.printEmployee();
  e2.printEmployee();
  Employee.printEmployeeGroup();
 // e1.printEmployeeGroup(); // compilation error
 // Employee.printEmployee(); // compilation error
}

Important points.

  • static keyword points to the current class
  • It applies to variables and methods in a class
  • static methods and variables are called directly using a class name, not an object instance.
  • Static variables has the same copy of data for all instance of a class
  • static methods do not allow to use of instance variables