import ballerina/io;

// An `isolated` object’s mutable state is `isolated` from the
// rest of the program.
isolated class Counter {
    // `n` is a mutable field.
    private int n = 0;

    isolated function get() returns int {
        lock {
            // `n` can only be accessed using `self`.
            return self.n;

        }
    }

    isolated function inc() {
        lock {
            self.n += 1;
        }
    }
}

public function main() {
    // The object’s mutable state is accessible only via the
    // object itself making it an “isolated root”.
    Counter c = new;

    c.inc();
    int v = c.get();
    io:println(v);
}

Isolated Objects

An object defined as isolated is similar to a module with isolated module-level variables. Mutable fields of an isolated object,

  • must be private and so can only be accessed using self
  • must be initialized with an isolated expression
  • must only be accessed within a lock statement
  • lock statement must follow the same rules for self as for an isolated variable
  • field is mutable unless it is final and has type that is subtype of readonly


Isolated root concept treats isolated objects as opaque. Isolated functions can access a final variable whose type is an isolated object.

import ballerina/io;
isolated class Counter {

An isolated object’s mutable state is isolated from the rest of the program.

    private int n = 0;

n is a mutable field.

    isolated function get() returns int {
        lock {
            return self.n;

n can only be accessed using self.

        }
    }
    isolated function inc() {
        lock {
            self.n += 1;
        }
    }
}
public function main() {
    Counter c = new;

The object’s mutable state is accessible only via the object itself making it an “isolated root”.

    c.inc();
    int v = c.get();
    io:println(v);
}
bal run isolated_objects.bal
1