Swan Lake Beta3 is here.
import ballerina/io;

class MyClass {
    int n;

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

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

public function main() {
    // Apply `new` operator to a class to get an object.
    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

Object is a separate basic type. An object value has named methods and fields. Methods and fields are in the same symbol space. A class both defines an object type and provides a way to construct an object. Apply new operator to 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 new operator to a class to get an object.

    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
import ballerina/io;

class MyClass {
    int n;

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

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

public function main() {
    // Apply `new` operator to a class to get an object.
    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

Object is a separate basic type. An object value has named methods and fields. Methods and fields are in the same symbol space. A class both defines an object type and provides a way to construct an object. Apply new operator to 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 new operator to a class to get an object.

    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
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand