import ballerina/io;

class MyClass {
    int n;

    function init(int n) {
        self.n = n;
    }

    function func() {
        self.n += 1;
    }
}

public function main() {
    // Apply the `new` operator with a `class` to get an `object` value.
    MyClass x = new MyClass(1234);

    // Call method using `obj.foo(args)`.
    x.func();

    // Access field using `obj.x`.
    int n = x.n;

    io:println(n);
}

Object

The object type is a separate basic type. An object value has named methods and fields. Methods and fields are in the same symbol space. A class defines an object type and provides a way to construct an object. Apply the new operator with a class to get an object. Call method using obj.foo(args). Access field using obj.x.

import ballerina/io;
class MyClass {
    int n;
    function init(int n) {
        self.n = n;
    }
    function func() {
        self.n += 1;
    }
}
public function main() {
    MyClass x = new MyClass(1234);

Apply the new operator with a class to get an object value.

    x.func();

Call method using obj.foo(args).

    int n = x.n;

Access field using obj.x.

    io:println(n);
}
bal run object.bal
1235