100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Classes and Objects in Dart

Understand how Dart classes act as blueprints for objects, covering fields, methods, instantiation, and the distinction between instance and static members.

Collections & OOPBeginner9 min readJul 10, 2026
Analogies

Classes and Objects in Dart

Object-oriented Dart revolves around two ideas: a class is a blueprint defining fields (data) and methods (behavior), and an object is a concrete instance created from that blueprint. Every object built from the same class shares the class's structure but carries its own independent state.

🏏

Cricket analogy: A cricket team's official player template, position, batting average, bowling style, is like a Dart class, and each real player such as Virat Kohli or Jasprit Bumrah is an object instantiated from that blueprint with their own specific stats.

Defining a Class

You define a class with the class keyword, listing fields and methods inside curly braces. A class specifies the shared shape every instance will have; each object built from it then carries independent values for those fields, even though the method logic is defined once on the class.

🏏

Cricket analogy: Defining class Batsman { String name; int runs; void score(int r) { runs += r; } } mirrors how a scorer's rulebook defines what data (name, runs) and actions (scoring) every batsman object must support.

dart
class Batsman {
  String name;
  int runs;

  Batsman(this.name, this.runs);

  void score(int r) {
    runs += r;
    print('$name now has $runs runs');
  }
}

void main() {
  var kohli = Batsman('Virat Kohli', 0);
  kohli.score(50);
  kohli.score(37);
  print(kohli.runs); // 87
}

Creating and Using Objects

You create an object by calling the class name as if it were a function, ClassName(args); the new keyword is optional in modern Dart. You then read and write its members with dot notation, object.field or object.method(). Crucially, each object keeps its own independent copy of instance fields.

🏏

Cricket analogy: Calling var kohli = Batsman('Virat Kohli', 0); creates one independent object, and kohli.score(50) updates only Kohli's runs; a separate object like var rohit = Batsman('Rohit Sharma', 0); keeps its own runs untouched.

In Dart, the new keyword is optional. Batsman('Virat Kohli', 0) and new Batsman('Virat Kohli', 0) are equivalent. Modern Dart style omits new entirely.

Instance vs Static Members

A field or method marked static belongs to the class itself rather than to any individual instance, so it is shared across every object and accessed via ClassName.member instead of an object reference. Instance members, by contrast, hold a separate value per object and require an object reference to access.

🏏

Cricket analogy: A static field like Team.totalMatchesPlayed belongs to the Team class itself and is shared across every team object, unlike an instance field like team.currentScore which is unique to that specific team's ongoing match.

dart
class Team {
  static int totalMatchesPlayed = 0;
  String teamName;
  int currentScore = 0;

  Team(this.teamName);

  void playMatch() {
    currentScore += 10;
    Team.totalMatchesPlayed++;
  }
}

void main() {
  var india = Team('India');
  var australia = Team('Australia');
  india.playMatch();
  australia.playMatch();
  print(Team.totalMatchesPlayed); // 2 (shared across all objects)
  print(india.currentScore);      // 10 (unique to india)
}

Encapsulation with Getters and Setters

Making all fields public, as in the examples above, is fine for learning, but production Dart code typically prefixes internal fields with an underscore, like _balance, to make them library-private, then exposes controlled access via getters and setters.

  • A class is a blueprint that defines fields (data) and methods (behavior); an object is a concrete instance created from that blueprint.
  • Each object has its own independent copy of instance fields; changing one object's field never affects another object of the same class.
  • The new keyword is optional in modern Dart; ClassName(args) alone creates an instance.
  • static fields and methods belong to the class itself and are shared across all instances, accessed via ClassName.member.
  • Instance members are accessed via an object reference using dot notation, like object.method().
  • Fields prefixed with an underscore (_field) are library-private in Dart, a common encapsulation convention.
  • Dot notation (object.field, object.method()) is how you read, write, and invoke an object's members.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#DartStudyNotes#ClassesAndObjectsInDart#Classes#Objects#Dart#Defining#OOP#StudyNotes#SkillVeris