0

I have this simple script

class Foo {
  int x;
  int y = 3;
  Foo(this.x) {
    x = x * 10;
    y = y * 10;
    print('constructed!');
  }
}

void main() {
  Foo myFoo = Foo(2);
  print(myFoo.x);
  print(myFoo.y);
}

And this

class Foo {
  int x;
  int y;
  Foo(this.x) : y = 3 {
    x = x * 10;
    y = y * 10;
    print('constructed!');
  }
}

void main() {
  Foo myFoo = Foo(2);
  print(myFoo.x);
  print(myFoo.y);
}

Both are giving same output, but what is the difference, mainly about initializer with colon symbol :. Focus with properties y, I declare/initialize y value to 3 in first script. I think I understand that's mean.

How about second script? y value is assigned at initializer with colon symbol to 3, what the difference? I'm trying understand initializer but still don't understand. What the purpose to create it? What is the equivalence of initializer compared to another program language like python , what case that using initializer?

0 Answers0