# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-1/static-methods-and-class.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
