×

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.


Related Topics

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.