Dart this Keyword
The current class object is referred to using the this keyword. It displays the class, methods, or constructor instance that is active at that moment. Calling the constructors or methods of the current class is another way to use it. By having the parameter names match, it removes any doubt regarding the class attributes. This keyword can eliminate confusion in the program by prefixing the class attributes. If we declare the class attributes the same as the parameter name, that will lead to ambiguity. It can be supplied as an argument to constructors or class methods.
Example
class Mobile {
String modelname;
int man_year;
// Creating constructor
Mobile(modelname, man_year){
modelname = modelname;
man_year = 2020;
print("Mobile's model name is: ${modelname}, and the manufacture year is: ${man_year}");
}
}
void main(){
Mobile mob = new Mobile("iPhone 11 ",2020);
}
Output
Mobile's model name is: iPhone 11 , and the manufacture year is: 2020
Explanation
We generated a class named Mobile in the program mentioned above, and it has two attributes: modelname and man_year. The constructor was then formed, and parameters were passed in with the same name as the class attributes.
The constructor parameters of the same name are used in the constructor body to assign values to the class variables (also known as attributes) on the left. A class’s constructor automatically calls itself when an instance is created, printing the outcome.
If there are multiple parameters with the same name, the Dart compiler may become confused. As a result, confusion will be produced by the compiler. For this reason, we refer to the current class object using the this keyword.
Example
class Mobile {
String modelname;
int man_year;
// Creating constructor
Mobile(modelname, man_year){
this.modelname = modelname;
this.man_year = 2020;
print("Mobile's model name is: ${modelname}, and the manufacture year is: ${man_year}");
}
}
void main(){
Mobile mob = new Mobile("IPhone 11",2020);
}
Output
Mobile's model name is: IPhone 11, and the manufacture year is: 2020
Explanation
this.modelname = modelname;
this.man_year = 2020;
The sole distinction between the previous program and the example above is this keyword.
This keyword has been used to a variable that is either an instance or a class apart from the local variable.
Points to Remember
- The current class object is pointed to using the this keyword.
- Referring to the current class variables is possible with it.
- This term allows us to instantiate or call the current class constructor.
- This keyword can be supplied as an argument when calling the constructor.
- This keyword can be supplied to the method call as an argument.
- It clears up any confusion or name conflicts in our instance’s or object’s constructor or method.
- The instance of the current class can be returned using it.
Local Variable
Blocks, constructors, and methods define local variables. It is formed when a constructor or method is created, and its scope is limited to those things. A local variable cannot be used outside of a constructor, block, or method.
Class Variable
The class variable, which is declared with the static keyword, is also referred to as the static member variable. Although it is not inside a constructor, function, or block, it is declared in the class. Class variables are common to all instances of that class, meaning that each instance has a single copy of the variable.
Instance Variable
The non-static variable, sometimes referred to as the instance variable, is one that is declared without the use of the static keyword. An object specifies the instance variables. The instance of that class can be used to access these variables.
Difference Between Class Variable and Instance Variable
The non-static variable, sometimes referred to as the instance variable, is one that is declared without the use of the static keyword. An object specifies the instance variables. The instance of that class can be used to access these variables.
Sr. | Class Variable | Instance Variable |
---|---|---|
1. | The class variable is declared using the static keyword in a class, but not in method and constructor. | The instance variable is declared in a class without using the static keyword. |
2. | The class variable can be accessed using the class name. Syntax: ClassName.variableName | The instance variable can be accessed using the instance of that class. Syntax: ObjectRefernce.variableName |
3 | The class variables are common to all instances of that class. All instances of the class share one copy of the static variable. | The instance variables are not common to all instance of class. Each object of particular will preserve its own copy of the instance variables. |
4 | These are created when the program is started and destroys when the program is terminated. | The instance variables are created when an object of the particular class created using the new() keyword and destroys when the object is destroyed. |