Table of contents
1.
Introduction
2.
"this" Keyword in Dart
2.1.
Implementation of “this” Keyword
3.
"Static" Keyword in Dart
3.1.
Static Variables
3.2.
Static Methods
3.3.
Implementation of Static Keyword
4.
Super Keyword in Dart
4.1.
Using Super Keyword with Variables
4.2.
Using Super Keyword with Methods
4.3.
Using Super Keyword with Constructors
5.
FAQs
5.1.
What is the difference between class variables and instance variables?
5.2.
Does each instance of a class possess a local copy of the static variables of the class?
5.3.
Can I access the overridden methods of the parent class in its child class?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart this, static, super keywords

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn about Dart keywords such as "this", "static", and “super”. The keyword this is used to distinguish between current class properties and the properties received as arguments. The keyword static will be used to declare static variables or methods. At last, we will use the keyword super, which will be used to distinguish between the parent and child class methods.

"this" Keyword in Dart

The “this” keyword is part of the OOPs concepts. We can access the properties and methods of the current class object with the help of the "this" keyword. It is mostly used in scenarios where the parameters passed to the methods have the same name as the properties or methods of that class. 

Implementation of “this” Keyword

The following is a sample program that will show us the usage of the “this” keyword in Dart. As you can see in this example, the constructor of our class named Ninja takes three arguments, namely ninja_name, ninja_batch and roll_no. You can also notice that these names match exactly the names of the properties of the Ninja class. So to distinguish between both of these sets of names, the “this” keyword comes into the picture. The names with which we attach the prefix “this” followed by a “dot” mean that they are referencing the properties of the class.

Code:

class Ninja{
  late String ninja_name;
  late String ninja_batch;
  late int roll_no;

  Ninja(String ninja_name, String ninja_batch, int roll_no){
    this.ninja_name = ninja_name;
    this.ninja_batch = ninja_batch;
    this.roll_no = roll_no;
  }

  void get_ninja_details(){
    print("Ninja name: " + ninja_name);
    print("Ninja batch: " + ninja_batch);
    print("Ninja roll no: ${roll_no}");
  }
}

void main() {
  Ninja obj = new Ninja("Mr X", "A1", 10);
  obj.get_ninja_details();
}

Output:

Ninja name: Mr X
Ninja batch: A1  
Ninja roll no: 10

"Static" Keyword in Dart

We can use the keyword "static" to declare class variables and methods. The variables and methods that are declared static are part of the global memory and do not belong to any individual instance of the class. Since the static methods/variables do not belong to any single instance, we can access them using the class name followed by the dot operator. We will look at more details in the next sub-section.

Static Variables

The variables declared using the keyword static are called static variables. All instances of the class modify the same copy of the static variable. Thus memory allocation is done only once when the class is actually loaded. Static variables are also known as class variables. 

Syntax:

static [data_type] variable_name;
To access the static variable, we can use the following syntax.
className.variable_name

Static Methods

Similar to static variables, we have the static methods in Dart. They are also known as class methods. Static methods do not belong to any specific instance of the class. Rather, they belong to the class itself. The static methods can access only the static variables and static member functions of the class. The static methods can be accessed by using the class name followed by the dot operator.

Syntax:

static [return_data_type] method_name(){
// method body
}

To access the static methods, we can use the following syntax.

className.static_method_name();

Implementation of Static Keyword

In this section, we will compile our learning by looking at an example that will show the usage of the static variables and static methods.

Code:

// Dart program to demonstrate the use of static variables and methods

class Ninja {
  // Static variable
  static late String ninja_department;
  // Non static variables
  late String ninja_name;
  late String ninja_batch;
  late int roll_no;

  // Non static method
  void get_ninja_details(){
    print("Ninja department: "+ Ninja.ninja_department); // Non static method can access static variables
    print("Ninja name: "+ ninja_name);
    print("Ninja batch: "+ ninja_batch);
    print("Ninja roll no: ${roll_no}");
  }

  // Static method
  static void demo_static_method(){
    // Static methods can access only the static variables
    print("Ninja department: "+ Ninja.ninja_department);
  }
}

void main() {
  Ninja obj = new Ninja();
  Ninja.ninja_department = "Robotics"; // Using class name to access static variable
  obj.ninja_name = "Mr X";
  obj.ninja_batch = "A1";
  obj.roll_no = 10;
  obj.get_ninja_details();
  Ninja.demo_static_method();
}

Output:

Ninja department: Robotics
Ninja name: Mr X
Ninja batch: A1
Ninja roll no: 10
Ninja department: Robotics

Super Keyword in Dart

The “super” keyword is used to access the immediate parent class's member properties, methods, and constructors. Its role is slightly similar to the “this” keyword because of the way the “this” keyword removes confusion between arguments to the function and local class properties. The “super” keyword is also used to remove confusion between child class properties and parent class properties when they possess the same names.  

Using Super Keyword with Variables

The following is the syntax to use the keyword “super” with variable names.

Syntax:

super.var_name

We can better understand this concept with the help of the following example.

Code:

class Student{
  late String gender;
  late String ninja_name;
}

class Ninja extends Student{
  late String ninja_batch;
  late int roll_no;

  Ninja(String gender, String ninja_name, String ninja_batch, int roll_no){
    // Using super keyword in the child class to access properties of the parent class (gender, ninja_name)
    super.gender = gender;
    super.ninja_name = ninja_name;
    this.ninja_batch = ninja_batch;
    this.roll_no = roll_no;
  }

  void get_ninja_details(){
    print("Ninja Gender: " + super.gender);
    print("Ninja name: " + super.ninja_name);
    print("Ninja batch: " + ninja_batch);
    print("Ninja roll no: ${roll_no}");
  }
}

void main() {
  Ninja obj = new Ninja("Male","Mr X", "A1", 10);
  obj.get_ninja_details();
}

Output:

Ninja Gender: Male
Ninja name: Mr X 
Ninja batch: A1  
Ninja roll no: 10

Using Super Keyword with Methods

The following is the syntax to use the keyword “super” with methods.

Syntax:

super.method_name

We can better understand this concept with the help of the following example.

Code:

// Dart program to demonstrate the usage of the super method with methods

class Student{
  void say_hello(){
    print("Hello from class Student");
  }
}

class Ninja extends Student{

  Ninja();  

  // This function is overridden in the child class
  void say_hello(){
    print("Hello from class Ninja");
  }

  void super_keyword_usage_example(){
    this.say_hello(); // Calling the current class say_hello function

    super.say_hello(); // Calling the say_hello function of the parent class

  }
}

void main() {
  Ninja obj = new Ninja();
  obj.super_keyword_usage_example();
}

Output:

Hello from class Ninja
Hello from class Student

Using Super Keyword with Constructors

The following is the syntax to use the keyword “super” with constructors, which are a special case of methods with no explicit return type.

Syntax:

:super();

We can better understand this concept with the help of the following example.

Code:

class Student{
  Student(){
    print("Constructor of class Student");
  }
}

class Ninja extends Student{
  Ninja():super(){
    print("Constructor of class Ninja");
  }
}

void main() {
  new Ninja();
}

Output:

Constructor of class Student
Constructor of class Ninja

FAQs

What is the difference between class variables and instance variables?

Class variables are the static member variables that are declared using the keyword static. On the other hand, instance variables are non-static variables specific to each object of that class.

 

Does each instance of a class possess a local copy of the static variables of the class?

No, all instances of a class share the same single master copy of the static variables.

 

Can I access the overridden methods of the parent class in its child class?

Yes, you can access the overridden methods of the parent class in its child class using the super keyword.

Conclusion

In this article, we have extensively discussed the keywords "this", "static", and "super" and saw example programs having their implementation in DartFeel free to read the blog Dart Getters and Setters on the Coding Ninjas Website to learn an interesting new concept in Dart.
We hope this blog has helped you enhance your knowledge regarding Dart this, static and super keywords. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass