×

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 Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

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 Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 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 - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

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.

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...

3 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.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 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 Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.