Dart Static Keyword (original) (raw)

Last Updated : 02 Apr, 2025

The **static keyword is used for the memory management of global data members. The static keyword can be applied to the fields and methods of a class. The static variables and methods are part of the class instead of a specific instance.

Dart Static Variables

The static variables belong to the class instead of a specific instance. A static variable is common to all instances of a class: this means only a single copy of the static variable is shared among all the instances of a class. The memory allocation for static variables happens only once in the class area at the time of class loading.

**Declaring Static Variables

Static variables can be declared using the static keyword followed by the data type and then the variable name.

static [date_type] [variable_name];

**Accessing Static Variable

The static variable can be accessed directly from the class name itself rather than creating an instance of it.

Classname.staticVariable;

Dart Static Methods

The static method belongs to a class instead of class instances. A static method is only allowed to access the static variables of a class and can invoke only static methods of the class. Usually, utility methods are created as static methods when we want them to be used by other classes without the need of creating an instance.

**Declaring Static Methods

A static method can be declared using the static keyword followed by return type, followed by method name.

static return_type method_name()
{
// Statement(s)
}

**Calling Static Method

Static methods can be invoked directly from the class name itself rather than creating an instance of it.

ClassName.staticMethod();

**Static Variable Usage

**Example :

Dart `

// Dart Program to show // Static methods in Dart class Employee { static var emp_dept; var emp_name; int emp_salary = 0;

// Function to show details
// of the Employee
showDetails() {
    print("Name of the Employee is: ${emp_name}");
    print("Salary of the Employee is: ${emp_salary}");
    print("Dept. of the Employee is: ${emp_dept}");
}

}

// Main function void main() { Employee e1 = new Employee(); Employee e2 = new Employee(); Employee.emp_dept = "MIS";

print("GeeksforGeeks Dart static Keyword Example");
e1.emp_name = 'Rahul';
e1.emp_salary = 50000;
e1.showDetails();

e2.emp_name = 'Tina';
e2.emp_salary = 55000;
e2.showDetails();

}

`

**Output:

GeeksforGeeks Dart static Keyword Example
Name of the Employee is: Rahul
Salary of the Employee is: 50000
Dept. of the Employee is: MIS
Name of the Employee is: Tina
Salary of the Employee is: 55000
Dept. of the Employee is: MIS

**Static Method Usage

**Example :

Dart `

// Dart program in dart to // illustrate static method class StaticMem { static int num = 0; static disp() { print("#GFG the value of num is ${StaticMem.num}"); } }

void main() { StaticMem.num = 75;

// initialize the static variable }
StaticMem.disp();

// invoke the static method

}

`

**Output:

#GFG the value of num is 75

Conclusion

The **static keyword in Dart helps optimize memory usage and simplifies access to class-level variables and methods. It enables data sharing across different instances, making it perfect for defining constants, utility functions, and shared resources. By utilizing static members, developers can create more efficient and organized code.