SWIG/Examples/java/variables/

Wrapping C Global Variables

When a C global variable appears in an interface file, SWIG tries to wrap it using a technique known as "variable linking." The idea is pretty simple---we try to create a Java variable that magically retrieves or updates the value of the underlying C variable when it is accessed. Click here to see a SWIG interface with some variable declarations in it.

Click here for the section on global variables in the SWIG and Java documentation.

Manipulating Variables from Java

C variables are accessed through getters and setters from Java. Unfortunately this is the only way to get current values from variables because it is not possible to overload the dot operator in Java. All global variables are accessible from the module class. For example if the module class is called 'example', the global variable
double foo;
will be accessed in the Java module as
example.get_foo();
example.set_foo(12.3);
Click here to see the example program that updates and prints out the values of the variables using this technique.

Key points

Creating read-only variables

The %immutable and %mutable directives can be used to specify a collection of read-only variables. For example:
%immutable;
int    status;
double blah;
...
%mutable;
The %immutable directive remains in effect until it is explicitly disabled using the %mutable directive.

Comments