Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page

Was this helpful?

  1. Dart Fundamentals-2
  2. Basic Concepts

Instance /Field Variables

PreviousObject/InstanceNextMethods

Last updated 4 years ago

Was this helpful?

We can access the instance variables with dot ( . )

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

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 with , an instance variable is a defined in a class (i.e. a ), for which each instantiated of the class has a separate copy, or instance. An instance variable is similar to a . 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. Source: Wikipedia

We can visualise the above code like below -

object-oriented programming
classes
variable
member variable
object
class variable
[1]