×

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 Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute 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 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 Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute 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 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 Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

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.

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

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

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 HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 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 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 Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

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