Dart Static Variables and Methods

Add Your Heading Text Here

Class is a template or prototype (or say defined model or structure) from which objects (instances) are created. Each instance of class have properties (variables) and behaviours (methods) defined in the prototype class. The scope of instance variables and methods are limited to particular instance’s lifetime only. So, it’s not possible to access or share a single variable to all the instances of a class using instance variables and instance methods etc.

Static Variables and Methods

Static variables and methods are mostly used to store information which can be shared among all the instances of class.

They are initialized only once, at the start of the program execution. Class variables and methods are initialized even before the instance variables or objects of the class.

Static Variables and methods are known as class variables and methods.

To declare a static variable or method static keyword is used in the program.

Static Variable Syntax :

static dataType varaiableName;

Static Methods Syntax :

static returnType methodName(){

// code block

}

Static variables and methods can’t be accessed creating instance of a class.

To access static variables and methods, call them with the className(in which particular static variable or method is declared).

For ex :

class SetTopBox{
  static String Channel;
  static changeChannel(String channelName){
    print("Change Channel : " + channelName);
  }
}
void main(){
// calling static variable Channel using className following . followed by variableName.
  SetTopBox.Channel="Sony TV";  
// calling static method changeChannel using className following . followed by methodName.
 print(SetTopBox.Channel);
  SetTopBox.changeChannel("Star TV");
}

Output :

Sony TV
Change Channel : Star TV

We can also share a static variable or method among different classes as well. To access a static variable or method from other class we need to call them using the className in which they are declared.

For ex :

class SetTopBox{
  static String Channel="Cartoon TV";
  static changeChannel(String channelName){
    print("Change Channel : " + channelName);
  }
}
class AndroidTV{
  AndroidTV(String channelName){
    print(SetTopBox.Channel);
    SetTopBox.changeChannel(channelName);
  }
}

void main(){
  AndroidTV TV=new AndroidTV("Discovery TV");
}

Output :

Cartoon TV
Change Channel : Discovery TV

In this way, we can easily share a variable and method across all the classes of our large projects. Which in turn makes our programming way more easy to handle and manipulate the data.

You must be well aware about static variables and methods for a better programming experience in your different projects.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons