Dart : Private Instance Variable

Dart : Private Instance Variable

·

1 min read

Private Instance Variable

class MyClass {
  // Private instance variable
  int _str;

  // Constructor
  MyClass(this._str);


  int get myPrivateVariable => _str;


  set value(int value) {
    _str = value;
  }

  // Method accessing the private variable
  void printPrivateVariable() {
    print(_str);
  }
}

void main() {
  var obj = MyClass(10);

  print(obj.myPrivateVariable); // Output: 10
  obj.printPrivateVariable();   // Output: 10


}