> For the complete documentation index, see [llms.txt](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-1/static-methods-and-class.md).

# Static Variables  and Methods

### &#x20;Static Variables

{% embed url="<https://youtu.be/BUtHUa9pi5k>" %}

{% tabs %}
{% tab title="Accessing static instance variables " %}

```dart
void main() {
  
  
   // Static instance variables 
  //-----------------------------
  
  var employee1 = Employee();
  
  
  
  employee1.id =1;
  employee1.name="John Doe";
  
  Employee.companyname="B";
    
  
  print(employee1.id);
  
  print(employee1.name);
  
  print(Employee.companyname);
  
 
}



class Employee{
  
  int id;
  
  String name;
  
  
  static String companyname="A";
  
 
  
}
```

{% endtab %}

{% tab title="Without creating an instance " %}

```dart
void main() {
  
  
   // Static instance variables 
  //-----------------------------
  
  
  Employee.companyname="B";
  
  print(Employee.companyname);

}



class Employee{
  
  int id;
  
  String name;
  
  
  static String companyname="A";
  
 
  
}
```

{% endtab %}

{% tab title="Accessing inside a method " %}

```dart
void main() {
  
  
   // Static instance variables 
  //-----------------------------
  
  var employee1 = Employee();
  
  
  
   employee1.id =1;
  employee1.name="John Doe";
  
  Employee.companyname="B";
    
  
//   print(employee1.id);
  
//   print(employee1.name);
  
  print(Employee.companyname);
  
  employee1.test();
  
}



class Employee{
  
  int id;
  
  String name;
  
  
  static String companyname="A";
  
  void test(){
  
     // Accessing  static fields inside a method 
    
    print(Employee.companyname);
    
    
    
  }
  
  
}
```

{% endtab %}
{% endtabs %}

{% hint style="success" %}

* [x] Static Variables are not related to object, they are related to Class
* [x] Static Variables are sometimes referred to as **Class variables**
* [x] There is no need for creating an object to access static variables&#x20;
* [x] You can't access static variables via instances&#x20;
* [x] Single copy of static variables is shared among all the instances
* [x] The purpose of using static variables is for memory management and memory efficiency&#x20;
* [x] Static Variables are not initialised (i.e. not given a memory segmentation ) unless they are used in the program \[  **Lazy initialisation** ]
  {% endhint %}

![](/files/-MDy3WaCe-bOtv3Y0JnA)

### &#x20;Static methods&#x20;

{% embed url="<https://youtu.be/rtUvYKq7X1I>" %}

{% tabs %}
{% tab title="Invoking static methods " %}

```dart
void main() {
  
  
   // Static methods  
  //-----------------------------
  
  var employee1 = Employee();
  
  employee1.id =1;
  employee1.name ="John Doe";
  
  Employee.salary(30);
  
}



class Employee{
  
  int id;
  
  String name;
  
 
  
  static void salary(int perDayWage){
    
      
     print("Salary : ${perDayWage*20}");
          
   
  }
  
  
  
 
  
}
```

{% endtab %}

{% tab title="Invoking without creating an instance" %}

```dart
void main() {
   
  Employee.salary(30);

}

class Employee{
  
  int id;
  
  String name;
  
  static void salary(int perDayWage){
      
     print("Salary : ${perDayWage*20}");
          
   
  }
  
 
  
}
```

{% endtab %}
{% endtabs %}

![](/files/-ME0XyHmfDeXNkLSIN-s)

{% hint style="success" %}

* [x] Static methods  are not related to objects , they are related to Class
* [x] Static methods are sometimes referred as **Class methods**&#x20;
* [x] There is no need for creating an instance to invoke a static method&#x20;
* [x] You can't invoke static method on an instance&#x20;
* [x] You cannot invoke a method from within a  static method
* [x] You can't access instance variables from within static methods , you can't even use "this" keyword inside static methods&#x20;
  {% endhint %}
