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.

class Person { // Fields (instance variables) String name; int age; // Constructor Person(this.name, this.age); // Method (instance method) void displayInfo() { print('Name: $name, Age: $age'); } // Method to change the person's age void haveBirthday() { age++; print('Happy Birthday, $name! You are now $age years old.'); } // Static method (does not need an instance) static String greet(String name) { return 'Hello, $name!'; } } void main() { // Creating an object of the Person class var person1 = Person('Alice', 30); var person2 = Person('Bob', 25); // Accessing fields print('Person 1 - Name: ${person1.name}, Age: ${person1.age}'); print('Person 2 - Name: ${person2.name}, Age: ${person2.age}'); // Calling instance methods person1.displayInfo(); // Output: Name: Alice, Age: 30 person2.displayInfo(); // Output: Name: Bob, Age: 25 // Calling method to update the age (birthday) person1.haveBirthday(); // Output: Happy Birthday, Alice! You are now 31 years old. // Accessing static method (no instance needed) print(Person.greet('Charlie')); // Output: Hello, Charlie! }

Explanation of the Code:

Fields:

  • String name; and int age; are fields. These store the name and age of a person. Each instance of the Person class will have its own name and age.
  • Fields can be initialized using a constructor, as shown in Person(this.name, this.age).

Methods:

  1. 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.
  2. Static Methods:

    • greet() is a static method. It can be called directly on the class (Person.greet('Charlie')) without creating an instance of Person. Static methods don't have access to instance variables, so they can't operate on name and age.

Constructor:

  • The constructor Person(this.name, this.age) initializes the name and age fields when a new object is created.

Creating Objects:

  • var person1 = Person('Alice', 30); creates an instance of Person with the name Alice and age 30.
  • var person2 = Person('Bob', 25); creates another instance with the name Bob and age 25.

Accessing and Modifying Fields:

  • The fields are accessed using the object reference (person1.name and person1.age).

Static Method:

  • The static method greet() is called using the class name: Person.greet('Charlie'). It doesn’t require an instance of the Person class to be invoked.

Output:

Person 1 - Name: Alice, Age: 30 Person 2 - Name: Bob, Age: 25 Name: Alice, Age: 30 Name: Bob, Age: 25 Happy Birthday, Alice! You are now 31 years old. Hello, Charlie!

Summary:

  • Fields store data specific to an instance (like name and age in the Person 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.