"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 Dart. Feel 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!