Defining a Class With Fields
Bundle state into a class.
Defining a Class With Fields is a free Dart Academy lesson on CoddyKit — lesson 1 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.
What a Class Is
A class is a blueprint for your own data type. It bundles related values together so you can model real things in code.
Your First Class
Declare a class with the class keyword and a capitalized name. The body, even if empty for now, lives inside the curly braces.
class Dog {
}Fields Hold State
Variables declared inside a class are called fields. They describe the data each object of that class will carry.
class Dog {
String name = "Rex";
int age = 3;
}Naming the Blueprint
By convention a class name uses UpperCamelCase, like User or BankAccount. This makes types easy to spot in your code.
Creating an Instance
Making an object from a class is called creating an instance. Just call the class name like a function to get one.
var d = Dog();Reading a Field
Use dot notation to read a field on an instance. The dot reaches into the object and pulls out its stored value.
var d = Dog();
print(d.name);Updating a Field
You can also assign with the dot to change a field. Each instance keeps its own copy, so changes stay local to that object.
var d = Dog();
d.age = 4;Each Instance Is Separate
Two objects from the same class are independent. Changing a field on one never affects the other, since each owns its own data.
var a = Dog();
var b = Dog();
a.name = "Max";Default Field Values
A field can start with a default value. New instances inherit that value until you assign something different later.
class Counter {
int count = 0;
}Nullable and Late Fields
If a field has no early value, mark it nullable with a question mark or use late to promise you set it before use.
class User {
String? nickname;
late String id;
}Why Classes Help
Grouping data into a class keeps related values together and named clearly. Your code becomes easier to read, reuse, and grow. 🧱
Quick Check
You made two Dog objects and changed one. What happens to the other?
Recap: Classes and Fields
You learned that a class is a blueprint, fields hold per-instance state, and each object you create keeps its own data. 🎉
Frequently asked questions
Is the “Defining a Class With Fields” lesson free?
Yes — the full text of “Defining a Class With Fields” 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 “Defining a Class With Fields”?
Bundle state into a class. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining a Class With Fields” 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
- Defining a Class With Fields
- Instance Methods and this
- Getters and Setters
- Static Members and Class Constants