Previous: Using pragma foreign_decl for Java, Up: Interfacing with Java   [Contents]


15.10.3.7 Using pragma foreign_code for Java

The Java code from ‘pragma foreign_proc’ declarations for Java will be placed in the bodies of static member functions of an automatically generated Java class. ‘pragma foreign_code’ can be used to define additional members of this automatically generated class, which can then be referenced by ‘pragma foreign_proc’ declarations for Java from that module.

For example:

:- pragma foreign_code("Java", "
    static int counter = 0;
").

:- impure pred incr_counter is det.
:- pragma foreign_proc("Java",
    incr_counter,
    [will_not_call_mercury],
"
    counter++;
").

:- semipure func get_counter = int.
:- pragma foreign_proc("Java",
    get_counter = (Result::out),
    [will_not_call_mercury, promise_semipure],
"
    Result = counter;
").