Brian Oh
Brian Oh

Reputation: 10720

Class instantiation in Dart and sharing code between Class objects

When a class that contains code is instantiated, is the code is the class shared automatically by other instantiations of that same class? Eg. The data in the class that is instantiated may be minimal; however the code could be very significant. If the code is not "automatically" shared, is there a way to achieve that other than separating the code from the class data?

Upvotes: 0

Views: 171

Answers (1)

Zdeslav Vojkovic
Zdeslav Vojkovic

Reputation: 14581

Sure.

Classes have the state and the behavior.

The state is encoded in member variables of the class. Each instance has its own copy of variables, thus its own state.

The behavior is specified by the methods implemented in the class ('methods' here stand for all static, non-static methods, setters and getters). Implementation is shared by all instances of the class, so all instances behave the same, but actual results and side-effects depend on instance state, obviously.

Upvotes: 1

Related Questions