# Class ConstantValue

java.lang.Object
org.apache.bcel.classfile.Attribute
org.apache.bcel.classfile.ConstantValue

All Implemented Interfaces:
  `Cloneable, Node`

public final class ConstantValue extends Attribute
This class is derived from Attribute and represents a constant value, that is, a default value for initializing a class field. This class is instantiated by the Attribute.readAttribute() method.

    ConstantValue_attribute {
      u2 attribute_name_index;
      u4 attribute_length;
      u2 constantvalue_index;
    }

See Also:
  - `Attribute`

- ## Field Summary

  ### Fields inherited from class Attribute

  `constant_pool, EMPTY_ARRAY, length, name_index, tag`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ConstantValue(int nameIndex, int length, int constantValueIndex, ConstantPool constantPool)`
  Constructs a ConstantValue.
  `ConstantValue(ConstantValue c)`
  Initialize from another object.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `accept(Visitor v)`
  Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  `Attribute`
  `copy(ConstantPool constantPool)`
  Creates a deep copy of this attribute.
  `void`
  `dump(DataOutputStream file)`
  Dumps constant value attribute to file stream on binary format.
  `int`
  `getConstantValueIndex()`
  Gets the index in constant pool of constant value.
  `void`
  `setConstantValueIndex(int constantValueIndex)`
  Sets the constant value index.
  `String`
  `toString()`

  ### Methods inherited from class Attribute

  `addAttributeReader, addAttributeReader, clone, getConstantPool, getLength, getName, getNameIndex, getTag, println, readAttribute, readAttribute, removeAttributeReader, setConstantPool, setLength, setNameIndex`

  ### Methods inherited from class Object

  `equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### ConstantValue

    public ConstantValue(ConstantValue c)
    Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical copy.
    Parameters:
      `c` - Source to copy.

  - ### ConstantValue

    public ConstantValue(int nameIndex, int length, int constantValueIndex, ConstantPool constantPool)
    Constructs a ConstantValue.
    Parameters:
      `nameIndex` - Name index in constant pool.
      `length` - Content length in bytes.
      `constantValueIndex` - Index in constant pool.
      `constantPool` - Array of constants.

- ## Method Details

  - ### accept

    public void accept(Visitor v)
    Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    Specified by:
      `accept` in interface `Node`
    Specified by:
      `accept` in class `Attribute`
    Parameters:
      `v` - Visitor object.

  - ### copy

    public Attribute copy(ConstantPool constantPool)
    Creates a deep copy of this attribute.
    Specified by:
      `copy` in class `Attribute`
    Parameters:
      `constantPool` - the constant pool.
    Returns:
      deep copy of this attribute.

  - ### dump

    public void dump(DataOutputStream file) throws IOException
    Dumps constant value attribute to file stream on binary format.
    Overrides:
      `dump` in class `Attribute`
    Parameters:
      `file` - Output file stream.
    Throws:
      `IOException` - if an I/O error occurs.

  - ### getConstantValueIndex

    public int getConstantValueIndex()
    Gets the index in constant pool of constant value.
    Returns:
      Index in constant pool of constant value.

  - ### setConstantValueIndex

    public void setConstantValueIndex(int constantValueIndex)
    Sets the constant value index.
    Parameters:
      `constantValueIndex` - the index info the constant pool of this constant value.

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Attribute`
    Returns:
      String representation of constant value.