×

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 Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

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.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

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

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 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 Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

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