ConstantInstruction (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.lang.classfile.instruction, interface: ConstantInstruction
Keywords: java.lang.classfile.instruction.ConstantInstruction interface

# Interface ConstantInstruction

All Superinterfaces:
  `ClassFileElement, CodeElement, Instruction`

All Known Subinterfaces:
  `ConstantInstruction.ArgumentConstantInstruction, ConstantInstruction.IntrinsicConstantInstruction, ConstantInstruction.LoadConstantInstruction`

public sealed interface ConstantInstruction extends Instruction permits ConstantInstruction.IntrinsicConstantInstruction, ConstantInstruction.ArgumentConstantInstruction, ConstantInstruction.LoadConstantInstruction
Models a constant-load instruction in the `code` array of a `Code` attribute, including "intrinsic", "argument", and "load" constant instructions. Corresponding opcodes have a kind of `Opcode.Kind.CONSTANT`. Delivered as a `CodeElement` when traversing the elements of a `CodeModel`.

The loaded constant value is symbolically represented as a `ConstantDesc`:

    ConstantInstruction(ConstantDesc constantValue)

Sealed Class Hierarchy Graph:
Since:
  24
See Also:
  - `Opcode.Kind.CONSTANT`
  - `CodeBuilder::loadConstant`

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Interface
  Description
  `static interface`
  `ConstantInstruction.ArgumentConstantInstruction`
  Models an "argument constant" instruction, which encodes the constant value in the instruction directly.
  `static interface`
  `ConstantInstruction.IntrinsicConstantInstruction`
  Models an "intrinsic constant" instruction, which encodes the constant value in its opcode.
  `static interface`
  `ConstantInstruction.LoadConstantInstruction`
  Models a "load constant" instruction, which encodes the constant value in the constant pool.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `ConstantDesc`
  `constantValue()`
  Returns the constant value.
  `static ConstantInstruction.ArgumentConstantInstruction`
  `ofArgument(Opcode op, int value)`
  Returns an argument constant instruction.
  `static ConstantInstruction.IntrinsicConstantInstruction`
  `ofIntrinsic(Opcode op)`
  Returns an intrinsic constant instruction.
  `static ConstantInstruction.LoadConstantInstruction`
  `ofLoad(Opcode op, LoadableConstantEntry constant)`
  Returns a load constant instruction.
  `TypeKind`
  `typeKind()`
  Returns the computational type of the constant.

  ### Methods declared in interface Instruction

  `opcode, sizeInBytes`
  Modifier and Type
  Method
  Description
  `Opcode`
  `opcode()`
  Returns the operation of this instruction.
  `int`
  `sizeInBytes()`
  Returns the size in bytes of this instruction.

- ## Method Details

  - ### constantValue

    ConstantDesc constantValue()
    Returns the constant value.
    Returns:
      the constant value

  - ### typeKind

    TypeKind typeKind()
    Returns the computational type of the constant. This is derived from the `constantValue`.
    Returns:
      the computational type of the constant

  - ### ofIntrinsic

    static ConstantInstruction.IntrinsicConstantInstruction ofIntrinsic(Opcode op)
    Returns an intrinsic constant instruction.
    Parameters:
      `op` - the opcode for the specific type of intrinsic constant instruction, which must be of kind `Opcode.Kind.CONSTANT`
    Returns:
      an intrinsic constant instruction
    Throws:
      `IllegalArgumentException` - if the opcode does not represent a constant with implicit value

  - ### ofArgument

    static ConstantInstruction.ArgumentConstantInstruction ofArgument(Opcode op, int value)
    Returns an argument constant instruction.

    `value` must be in the range of `byte`, `[-128, 127]`, for `Opcode.BIPUSH`, and in the range of `short`, `[-32768, 32767]`, for `Opcode.SIPUSH`.

    Parameters:
      `op` - the opcode for the specific type of argument constant instruction, which must be `Opcode.BIPUSH` or `Opcode.SIPUSH`
      `value` - the constant value
    Returns:
      an argument constant instruction
    Throws:
      `IllegalArgumentException` - if the opcode is not `Opcode.BIPUSH` or `Opcode.SIPUSH`, or if the constant value is out of range for the opcode

  - ### ofLoad

    static ConstantInstruction.LoadConstantInstruction ofLoad(Opcode op, LoadableConstantEntry constant)
    Returns a load constant instruction.
    Parameters:
      `op` - the opcode for the specific type of load constant instruction, which must be of kind `Opcode.Kind.CONSTANT`
      `constant` - the constant value
    Returns:
      a load constant instruction
    Throws:
      `IllegalArgumentException` - if the opcode is not `Opcode.LDC`, `Opcode.LDC_W`, or `Opcode.LDC2_W`