Chapter 16: a class, built
Chapter 16 builds a class: Counter, counting ticks, and the program that runs it three times. The IDE's own tools make the structure - the class, the field's declaration, each method's whole signature, the program's shell - so what is left to type is only the logic no tool can write: what tick does, what total returns, what Show prints. To play it in the IDE: Help > Lessons > Writing code…, or ek9 -ide -lesson book-classes.
What you'll build
The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.
#!ek9 defines module book.counter defines class Counter count as Integer: 0 tick() count += 1 total() as pure <- rtn <- Integer(count) default operator ? defines program Show() stdout <- Stdout() clicks <- Counter() clicks.tick() clicks.tick() clicks.tick() stdout.println(`clicks so far: ${clicks.total()}`)
Step 1: A class
Choose Code > New Construct > Class…
Counter is the class's name. Closed is already selected - EK9's default: a class is closed to extension unless it says 'as open' or 'as abstract'. Type Counter and choose Preview.
The preview shows Counter about to be written - a shell, nothing inside it yet; then the IDE formats the file it writes. Choose Apply.
Step 2: A field
Right-click the Counter line and choose Add Field…
Type count as Integer: 0 - a field, its type, and the value it starts with. Choose OK.
The preview shows count added - and default operator ? with it: a class that holds fields must say when it counts as set (EK9's tri-state rule), so adding the first field added that operator too. Choose Apply.
Step 3: tick — and its logic
Right-click the Counter line and choose Add Method…
tick is the method's name - no return, no parameters, not pure: it changes count. Type tick and choose Apply.
Select the stub, from ready <- true down to its //AI line, and type count += 1 - tick's whole job. Saves when typed.
Step 4: total — and its logic
Right-click the Counter line and choose Add Method…
total is the name, Integer the return type, and pure ticked - total only reads count, it changes nothing. Fill them in and choose Apply.
Select the stub, from <- rtn as Integer: 0 down to its //AI line, and type <- rtn <- Integer(count) - total hands back count, as an Integer in its own right. Saves when typed.
Step 5: The program
Choose Code > New Construct > Program…
Show is the program's name - no application to bind, no parameters, nothing injected: the plain shape every Getting Started program starts from. Type Show and choose Preview.
The preview shows Show about to be written. Choose Apply.
Select the stub, from ready <- true down to its //AI line, and type Show's body: a Stdout, a Counter, three ticks, and a line printing the total. Saves when typed.
Step 6: Run it
Choose Run > Run Program…
The project's only program is already chosen. Choose Run.
clicks so far: 3 - tick() ran three times, and total() reads count back as an Integer. Choose Close.
The result
Counter.ek9 now reads exactly as chapter 16 of Getting Started with EK9 shows it: a class the tools built, logic you typed. The same four tools - ek9_new_construct, ek9_add_field, ek9_add_method, ek9_add_program - are what an AI assistant calls to do the same thing.
Where this lesson comes from
- For AI assistants - the MCP tool
ek9_new_construct,ek9_add_field,ek9_add_method,ek9_add_program.



















