# Instance /Field Variables

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

{% hint style="info" %}
We can access the  instance variables with    dot ( . )&#x20;
{% endhint %}

{% hint style="danger" %}
By default instance variables are set to "null" . But from Dart 2.9 with null safety  you can't  do this . It will show you an error&#x20;
{% endhint %}

```dart
void main() {
  
  
  
  var employee1 =   Employee();  // Creating employee1 object   
  
  var  employee2 =  Employee(); // Creating employee2  object 
  
  
  
  //Attribute <=> Instance Variable <=> Field Variable 
  
  
   // Setting the instance variable of employee1 object 
   
  employee1.id =1;
  employee1.name ="John Doe";
  employee1.position ="CTO";
  employee1.companyname ="B"; // Overwritting companyname "A"
  
  
  
  employee2.id =2;
  employee2.name ="Jane Doe";
  employee2.position ="CEO";
  
 // companyname for employee2 will be set to "A" , because we 
 // didn't set it, it take the default value from class 
  
  
  
  
}

class Employee{
  
  
  int id;
  
  String name;
  
  String position;  

  String companyname = "A" ;
  
  
  
}
```

> In [object-oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming) with [classes](https://en.wikipedia.org/wiki/Class_\(computer_science\)), an **instance variable** is a [variable](https://en.wikipedia.org/wiki/Variable_\(programming\)) defined in a class (i.e. a [member variable](https://en.wikipedia.org/wiki/Member_variable)), for which each instantiated [object](https://en.wikipedia.org/wiki/Object_\(computer_science\)) of the class has a separate copy, or instance. An instance variable is similar to a [class variable](https://en.wikipedia.org/wiki/Class_variable).[\[1\]](https://en.wikipedia.org/wiki/Instance_variable#cite_note-1) An instance variable is a variable which is **declared in a class but outside of constructors, methods, or blocks**. Instance variables are created when an object is instantiated, and **are accessible to all the constructors, methods, or blocks in the class**.
>
> Each instance variable lives in memory for the life of the object it is owned by.\
> &#x20;<img src="https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MDR9Mmcax_zEDDFWpHw%2F-MDR9U2BtD1pOB-MZbdE%2Funnamed.png?alt=media&#x26;token=bcedd461-d11d-4256-81c5-fa8afd13df2a" alt="" data-size="line"> ***Source: Wikipedia***

#### <img src="https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MDR9Mmcax_zEDDFWpHw%2F-MDR9U2BtD1pOB-MZbdE%2Funnamed.png?alt=media&#x26;token=bcedd461-d11d-4256-81c5-fa8afd13df2a" alt="" data-size="line"> We can visualise the above code like below -&#x20;

![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MDGvIvzgx39SnJBDp7I%2F-MDH0rPocRYT0QVdG503%2Fgiphy.gif?alt=media\&token=dc7c2568-6b55-4d34-9561-7e825a0869d8)

![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MDGvIvzgx39SnJBDp7I%2F-MDH2ZIJ4LWhzU-B54bs%2Fgiphy2.gif?alt=media\&token=4b7f58be-ca21-41fe-ba12-6946316c2ae9)
