Instance Variables & Methods in Dart

Instance Variables & Methods in Dart

·

1 min read

Instance Variables

  • Instance variables represent the data, and methods represent the functions. We use the class keyword to define a class, followed by the class name and a pair of curly braces {}.

      class Point {
        int x;
        int y;
    
        void moveTo(int newX, int newY) {
          x = newX;
          y = newY;
        }
      }
    

Instance Method

  • Unless the method is declared as static it is classified as an instance method in a class. They are allowed to access instance variables.

class Jeet { 

    int a; 
    int b; 

    void sum(int c, int d) 
    { 
        this.a = c; 
        this.b = d; 

        print('Sum of numbers is ${a + b}'); 
    } 
} 

void main() 
{ 
    Jeet one = new Jeet(); 

    one.sum(21, 12); 

}