All Products
Search
Document Center

OpenSearch:Classes and objects

Last Updated:Apr 01, 2026

Cava supports object-oriented programming (OOP) with the same class and new syntax as Java and C#. Use the class keyword to define classes and the new keyword to create objects.

class Example {
    double PI;
    Example() {
        PI = 3.1415926;
    }
    static int main() {
        Example example = new Example();
        double a = example.PI; // 3.1415926
        return 0;
    }
}

Cava supports the core OOP concepts: encapsulation, abstraction, class, object, instance, and method.

Define a class

Use the class keyword to define a class. A class definition can include any combination of the following:

ComponentDescription
Member variableHolds data for each object instance.
ConstructorInitializes a new object immediately after creation.
Member functionEncapsulates operations on the object's data.
Class functionBelongs to the class itself and is shared across all instances.
class Example {                    // Define a class named Example.
    public double PI;              // Member variable
    Example() {                    // Constructor
        PI = 3.1415926;
    }
    double getPI() {               // Member function
        return PI;
    }
    static int main() {
        Example example = new Example();
        double a = example.getPI(); // 3.1415926
        return 0;
    }
}

Create an object

Creating an object from a class involves three steps:

  1. Declaration — declare a variable and specify its class type.

  2. Instantiation — use the new keyword to allocate the object.

  3. Initialization — specify a constructor after new to initialize the object.

Like Java and C#, Cava requires the new keyword to create an object. All three steps can be combined on a single line.
class Example {
    double PI;
    Example() {
        PI = 3.1415926;
    }
    static int main() {
        Example example = new Example(); // Declare, instantiate, and initialize in one line.
        return 0;
    }
}

After the object is created, use the variable (example) to access its member data.

Member variables

A member variable carries the data of the relevant object.

class Example {
    double PI; // Member variable of type double
    Example() {
        PI = 3.1415926;
    }
}
Initialize member variables in a constructor, not at the point of declaration. Assigning an initial value at declaration is not supported.
// Not supported
class Example {
    double PI = 3.1415926;
}

// Correct
class Example {
    double PI;
    Example() {
        PI = 3.1415926;
    }
}

Constructors

A constructor initializes an object right after it is created. Constructors have two requirements:

  • The constructor name must match the class name.

  • A constructor has no return value and no output parameters.

Cava supports multiple constructors (constructor overloading). The syntax of the new expression determines which constructor is called.

class Example {
    public double PI;
    Example() {                        // Constructor A: default
        this.PI = 3.1415926;
    }
    Example(double customPI) {         // Constructor B: accepts a custom value
        this.PI = customPI;
    }
    double getPI() {
        return PI;
    }
    static int main() {
        Example example1 = new Example();       // Calls Constructor A
        double a = example1.getPI();            // 3.1415926

        Example example2 = new Example(3.14);   // Calls Constructor B
        double b = example2.getPI();            // 3.14
        return 0;
    }
}

Member functions

A member function encapsulates operations on an object's member variables. Call a member function through an object instance to read or modify its data.

class Example {
    public double PI;
    Example() {
        this.PI = 3.1415926;
    }
    double getPI() {
        return PI;
    }
    static int main() {
        Example example = new Example();
        double a = example.getPI(); // 3.1415926
        return 0;
    }
}

Member functions support overloading: define multiple functions with the same name in a class, and the compiler selects the right one based on the type and number of input parameters.

When a local variable in a member function has the same name as a member variable, the local variable takes precedence. To access the member variable explicitly, prefix it with this.

Class functions

A class function belongs to the class, not to any individual object. It is shared by all instances and can be called directly through the class name — no object creation required.

Define a class function with the static keyword:

class Example {
    static double getPI() { // Class function — accessible without creating an object
        return 3.1415926;
    }
    static int main() {
        double a = Example.getPI(); // Call via class name, not an object instance
        return 0;
    }
}

To compare: member functions require an object instance; class functions do not.

Limitations

Cava does not support class inheritance.