Dart Fields and Methods
Fields and Methods of a Class in Dart
In Dart, fields and methods are the primary components of a class that define the structure and behavior of objects created from the class.
- Fields (also called properties or variables) are used to store the state or data of an object.
- Methods are functions that define the behavior of an object. They can operate on the fields of the class and perform actions.
Fields in Dart
Fields represent the state of an object. They are declared inside the class and can have different access modifiers (public or private). A field is typically initialized with a value in the constructor, or it can be initialized directly when declared.
Methods in Dart
Methods define the actions that can be performed on an object. They can access and modify the fields of the class. Methods can be instance methods (i.e., methods that operate on individual instances of the class) or static methods (i.e., methods that belong to the class itself and don't require an instance).
Example of Fields and Methods in Dart
Let’s create a Person
class to demonstrate fields and methods in Dart.
Explanation of the Code:
Fields:
String name;
andint age;
are fields. These store the name and age of a person. Each instance of thePerson
class will have its ownname
andage
.- Fields can be initialized using a constructor, as shown in
Person(this.name, this.age)
.
Methods:
Instance Methods:
displayInfo()
is an instance method that prints the name and age of the person. It is called using an object (person1.displayInfo()
).haveBirthday()
is another instance method that increments the age of the person and prints a birthday message.
Static Methods:
greet()
is a static method. It can be called directly on the class (Person.greet('Charlie')
) without creating an instance ofPerson
. Static methods don't have access to instance variables, so they can't operate onname
andage
.
Constructor:
- The constructor
Person(this.name, this.age)
initializes thename
andage
fields when a new object is created.
Creating Objects:
var person1 = Person('Alice', 30);
creates an instance ofPerson
with the nameAlice
and age30
.var person2 = Person('Bob', 25);
creates another instance with the nameBob
and age25
.
Accessing and Modifying Fields:
- The fields are accessed using the object reference (
person1.name
andperson1.age
).
Static Method:
- The static method
greet()
is called using the class name:Person.greet('Charlie')
. It doesn’t require an instance of thePerson
class to be invoked.
Output:
Summary:
- Fields store data specific to an instance (like
name
andage
in thePerson
class). - Methods define behavior and can modify or access fields. Instance methods are called on objects, while static methods are called on the class itself.
- Constructors are used to initialize fields when creating an object.
- Static methods don’t require an instance and are useful for operations that are related to the class but not to specific instances.
Understanding fields and methods is essential to working with classes and objects in Dart, as they define the structure (state) and functionality (behavior) of your objects.