September 2019
Beginner
512 pages
12h 52m
English
As you already know, fields are nothing more than variables that hold object values, and methods are simple functions that represent object actions. In some cases, you may want to share a value or method between all of the object instances of a class. For this use case, you can add the static modifier to them, as follows:
class Person { // ... class fields definition static String personLabel = "Person name:"; String get fullName => "$personLabel $firstName $lastName"; // modified to print the new static field "personLabel"}
Hence, we can change the static field value directly on the class:
main() { Person somePerson = Person("clark", "kent"); Person anotherPerson = Person("peter", "parker"); print(somePerson.fullName); ...Read now
Unlock full access