0Pricing
Dart Academy · Lesson

Instance Methods and this

Add behavior that acts on instance data.

Instance Methods and this is a free Dart Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Adding Behavior

Fields store data, but a class can also do things. A function defined inside a class is called an instance method.

Your First Method

Write a method just like a function, but inside the class body. It can read and use the object's own fields directly.

class Dog {
  String name = "Rex";
  void bark() {
    print("Woof!");
  }
}

Calling a Method

Use dot notation on an instance to run its method. The dot tells Dart which object should do the work.

var d = Dog();
d.bark();

Methods See Fields

Inside a method you can use a field by name. The method automatically acts on whichever instance you called it on.

void greet() {
  print("Hi, I am $name");
}

Meet this

The keyword this refers to the current instance, the very object the method is running on right now.

void greet() {
  print(this.name);
}

When this Is Optional

Often this is implied, so you can drop it. Writing name and this.name mean the same thing when there is no clash.

void greet() {
  print(name); // same as this.name
}

When this Is Required

You need this when a parameter shadows a field with the same name. It tells Dart you mean the field, not the parameter.

void rename(String name) {
  this.name = name;
}

Methods Take Parameters

A method can accept inputs just like any function. Combine those inputs with fields to produce useful results.

void addAge(int years) {
  age = age + years;
}

Methods Can Return Values

Give a method a return type and return a result. This lets one object compute and hand back a value to your code.

String label() {
  return "$name ($age)";
}

Methods Calling Methods

One method can call another on the same object. Just use the name directly, and this is understood for you.

void play() {
  bark();
  print("$name is playing");
}

Why Methods Matter

Methods keep behavior next to the data it uses. Your objects become smart, self-contained, and far easier to reason about. 🐕

Quick Check

A method has a parameter named the same as a field. How do you set the field?

Recap: Methods and this

You learned that methods add behavior, use fields directly, and that this points to the current instance when names clash. 🎉

Frequently asked questions

Is the “Instance Methods and this” lesson free?

Yes — the full text of “Instance Methods and this” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Instance Methods and this”?

Add behavior that acts on instance data. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Instance Methods and this” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining a Class With Fields
  2. Instance Methods and this
  3. Getters and Setters
  4. Static Members and Class Constants
← Back to Dart Academy