ConstantPool (Java SE 23 & JDK 23)
Description: declaration: module: java.base, package: java.lang.classfile.constantpool, interface: ConstantPool
Keywords: java.lang.classfile.constantpool.ConstantPool interface

# Interface ConstantPool

All Superinterfaces:
  `Iterable<PoolEntryPREVIEW>`

All Known Subinterfaces:
  `ClassReaderPREVIEW`, `ConstantPoolBuilderPREVIEW`

public sealed interface ConstantPool extends Iterable<PoolEntryPREVIEW> permits ClassReaderPREVIEW, ConstantPoolBuilderPREVIEW
`ConstantPool` is a preview API of the Java platform.
Programs can only use `ConstantPool` when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
Provides read access to the constant pool and bootstrap method table of a classfile.

See Java Virtual Machine Specification:
  4.4 The Constant Pool
Sealed Class Hierarchy Graph:
Since:
  22

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `bootstrapMethodCount()`
  Returns the number of entries in the bootstrap method table.
  `BootstrapMethodEntryPREVIEW`
  `bootstrapMethodEntry(int index)`
  Returns the `BootstrapMethodEntry`PREVIEW at the specified index within the bootstrap method table.
  `PoolEntryPREVIEW`
  `entryByIndex(int index)`
  Returns the entry at the specified index.
  `<T extends PoolEntryPREVIEW>
  T`
  `entryByIndex(int index, Class<T> cls)`
  Returns the entry of a given type at the specified index.
  `default Iterator<PoolEntryPREVIEW>`
  `iterator()`
  Returns an iterator over pool entries.
  `int`
  `size()`
  Returns the size of the constant pool.

  ### Methods declared in interface java.lang.Iterable

  `forEach, spliterator`

- ## Method Details

  - ### entryByIndex

    PoolEntryPREVIEW entryByIndex(int index)
    Returns the entry at the specified index.
    API Note:
      If only a particular type of entry is expected, use `entryByIndex(int, Class)`.
    Parameters:
      `index` - the index within the pool of the desired entry
    Returns:
      the entry at the specified index
    Throws:
      `ConstantPoolExceptionPREVIEW` - if the index is out of range of the constant pool, or is considered unusable

  - ### size

    int size()
    Returns the size of the constant pool.
    Returns:
      the size of the constant pool

  - ### entryByIndex

    <T extends PoolEntryPREVIEW> T entryByIndex(int index, Class<T> cls)
    Returns the entry of a given type at the specified index.
    Type Parameters:
      `T` - the entry type
    Parameters:
      `index` - the index within the pool of the desired entry
      `cls` - the entry type
    Returns:
      the entry of a given type at the specified index
    Throws:
      `ConstantPoolExceptionPREVIEW` - if the index is out of range of the constant pool, or the entry is not of the given type
    Since:
      23

  - ### iterator

    default Iterator<PoolEntryPREVIEW> iterator()
    Returns an iterator over pool entries.
    Specified by:
      `iterator` in interface `Iterable<PoolEntryPREVIEW>`
    Returns:
      an iterator over pool entries

  - ### bootstrapMethodEntry

    BootstrapMethodEntryPREVIEW bootstrapMethodEntry(int index)
    Returns the `BootstrapMethodEntry`PREVIEW at the specified index within the bootstrap method table.
    Parameters:
      `index` - the index within the bootstrap method table of the desired entry
    Returns:
      the `BootstrapMethodEntry`PREVIEW at the specified index within the bootstrap method table
    Throws:
      `ConstantPoolExceptionPREVIEW` - if the index is out of range of the bootstrap methods

  - ### bootstrapMethodCount

    int bootstrapMethodCount()
    Returns the number of entries in the bootstrap method table.
    Returns:
      the number of entries in the bootstrap method table