Swan Lake Beta3 is here.
import ballerina/io;

public class Counter {
    // `private` means accessible only by code within the class definition.
    private int n;

    // `init` method initializes the object.
    public function init(int n = 0) {
        self.n = n;
    }

    public function get() returns int {
        // Methods use `self` to access their object.
        return self.n;

    }

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

public function main() {
    // Arguments to `new` are passed as arguments to `init`.
    Counter counter = new (12);

    io:println(counter.get());
}

Defining Classes

Module can contain class definitions. init method initializes the object. Arguments to new are passed as arguments to init. Methods use self to access their object. private means accessible only by code within the class definition.

import ballerina/io;
public class Counter {
    private int n;

private means accessible only by code within the class definition.

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

init method initializes the object.

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

Methods use self to access their object.

    }
    public function inc() {
        self.n += 1;
    }
}
public function main() {
    Counter counter = new (12);

Arguments to new are passed as arguments to init.

    io:println(counter.get());
}
bal run defining_classes.bal
12
import ballerina/io;

public class Counter {
    // `private` means accessible only by code within the class definition.
    private int n;

    // `init` method initializes the object.
    public function init(int n = 0) {
        self.n = n;
    }

    public function get() returns int {
        // Methods use `self` to access their object.
        return self.n;

    }

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

public function main() {
    // Arguments to `new` are passed as arguments to `init`.
    Counter counter = new (12);

    io:println(counter.get());
}

Defining Classes

Module can contain class definitions. init method initializes the object. Arguments to new are passed as arguments to init. Methods use self to access their object. private means accessible only by code within the class definition.

import ballerina/io;
public class Counter {
    private int n;

private means accessible only by code within the class definition.

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

init method initializes the object.

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

Methods use self to access their object.

    }
    public function inc() {
        self.n += 1;
    }
}
public function main() {
    Counter counter = new (12);

Arguments to new are passed as arguments to init.

    io:println(counter.get());
}
bal run defining_classes.bal
12
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