import ballerina/io;
int total = 98;string content = "";function main (string[] args) {
    io:println(total);    content = content + "This is a sample text\n";
    io:println(content);
}

Global Variables

In Ballerina, global variables are defined as top level constructs. They are usually defined on top of the Ballerina program. The global variables hold their value throughout the lifetime of the Ballerina program.

import ballerina/io;
int total = 98;

Defining the global variables.

string content = "";
function main (string[] args) {
    io:println(total);

Accessing a global variable.

    content = content + "This is a sample text\n";
    io:println(content);
}
$ ballerina run global-variables.bal
98

Prints the value of the global variable ‘total’.

This is a sample text

Prints the updated value of the global variable ‘content’.