🛠️ P3/COBOL 25.1.0-SNAPSHOT is available for preview.
Reference Manual
Java Extension

Java Extensions

JAVA Statement

At any statement boundary within the PROCEDURE DIVISION, Java-language code may be inserted (injected) at the corresponding point of execution of the COBOL program. The context of the inserted Java code is that of a method body. In other words, the Java that may be used is any Java that would be valid within a Java instance method.

The format of the Java statement block is as follows:

JAVA[javaStatement]END-JAVA\mathsf{JAVA} \begin{bmatrix} \mathit{javaStatement} \cdots \end{bmatrix} \mathsf{END} \text{-} \mathsf{JAVA}

Within a statement-level Java block, the Java statement break java; may be used to immediately exit the current embedded Java code block and resume COBOL execution at that point.

💡

A statement-level or Java-section JAVA / END-JAVA pair delimits lines, not characters: JAVA and END-JAVA must each begin their own source line. That restriction does not apply to the CLASS JAVA ... END-JAVA form in the REPOSITORY paragraph, which is an ordinary COBOL clause and may appear entirely on one line.

Optional Java Section

An optional Java section may appear at the beginning of the PROCEDURE DIVISION, immediately after the division header and any USING / RETURNING / GIVING phrases, and before the declaratives or procedure body. In both traditional and JAS forms, the section is a braced JAVA ... END-JAVA block:

procedure division.
{
    java
        private int retryCount;
        private void resetRetry() {
            retryCount = 0;
        }
    end-java
}

Java enclosed in this section is inserted into the body of the generated Program subclass for the current program unit, at class-member level rather than inside a method. Fields, initializers, and methods are therefore valid; ordinary executable statements that would require a method body are not. Statement-level JAVA blocks elsewhere in the procedure division remain method-body injections.

In a JAS procedure division the same placement and semantics apply; see Optional Java section.

MicroJava Annotations

Java annotations may also be injected without a JAVA ... END-JAVA block by using microJava annotations: accent-grave delimited annotation text attached to a JAS procedure division or modern paragraph. See MicroJava Annotations in the JAS reference.