# Class FieldGen

java.lang.Object
org.apache.bcel.classfile.AccessFlags
org.apache.bcel.generic.FieldGenOrMethodGen
org.apache.bcel.generic.FieldGen

All Implemented Interfaces:
  `Cloneable, NamedAndTyped`

public class FieldGen extends FieldGenOrMethodGen
Template class for building up a field. The only extraordinary thing one can do is to add a constant value attribute to a field (which must of course be compatible with to the declared type).

See Also:
  - `Field`

- ## Field Summary

  ### Fields inherited from class FieldGenOrMethodGen

  `cp, name, type`

  ### Fields inherited from class AccessFlags

  `access_flags`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `FieldGen(int accessFlags, Type type, String name, ConstantPoolGen cp)`
  Declare a field.
  `FieldGen(Field field, ConstantPoolGen cp)`
  Instantiate from existing field.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addObserver(FieldObserver o)`
  Add observer for this object.
  `void`
  `cancelInitValue()`
  Remove any initial value.
  `FieldGen`
  `copy(ConstantPoolGen cp)`
  Creates a deep copy of this field.
  `boolean`
  `equals(Object obj)`
  Return value as defined by given BCELComparator strategy.
  `static BCELComparator<FieldGen>`
  `getComparator()`
  Gets the comparison strategy object.
  `Field`
  `getField()`
  Gets field object after having set up all necessary values.
  `String`
  `getInitValue()`
  Gets the initial value.
  `String`
  `getSignature()`
  Gets the signature of method or field.
  `int`
  `hashCode()`
  Return value as defined by given BCELComparator strategy.
  `void`
  `removeObserver(FieldObserver o)`
  Remove observer for this object.
  `static void`
  `setComparator(BCELComparator<FieldGen> comparator)`
  Sets the comparison strategy object.
  `void`
  `setInitValue(boolean b)`
  Sets the initial value.
  `void`
  `setInitValue(byte b)`
  Sets the initial value.
  `void`
  `setInitValue(char c)`
  Sets the initial value.
  `void`
  `setInitValue(double d)`
  Sets the initial value.
  `void`
  `setInitValue(float f)`
  Sets the initial value.
  `void`
  `setInitValue(int i)`
  Sets the initial value.
  `void`
  `setInitValue(long l)`
  Sets the initial value.
  `void`
  `setInitValue(short s)`
  Sets the initial value.
  `void`
  `setInitValue(String str)`
  Sets (optional) initial value of field, otherwise it will be set to null/0/false by the JVM automatically.
  `final String`
  `toString()`
  Return string representation close to declaration format, for example: 'public static final short MAX = 100'.
  `void`
  `update()`
  Call notify() method on all observers.

  ### Methods inherited from class FieldGenOrMethodGen

  `addAll, addAnnotationEntry, addAttribute, clone, getAnnotationEntries, getAttributes, getConstantPool, getName, getType, removeAnnotationEntries, removeAnnotationEntry, removeAttribute, removeAttributes, setConstantPool, setName, setType`

  ### Methods inherited from class AccessFlags

  `getAccessFlags, getModifiers, isAbstract, isAbstract, isAnnotation, isAnnotation, isEnum, isEnum, isFinal, isFinal, isInterface, isInterface, isNative, isNative, isPrivate, isPrivate, isProtected, isProtected, isPublic, isPublic, isStatic, isStatic, isStrictfp, isStrictfp, isSynchronized, isSynchronized, isSynthetic, isSynthetic, isTransient, isTransient, isVarArgs, isVarArgs, isVolatile, isVolatile, setAccessFlags, setModifiers`

  ### Methods inherited from class Object

  `finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### FieldGen

    public FieldGen(Field field, ConstantPoolGen cp)
    Instantiate from existing field.
    Parameters:
      `field` - Field object.
      `cp` - constant pool (must contain the same entries as the field's constant pool).

  - ### FieldGen

    public FieldGen(int accessFlags, Type type, String name, ConstantPoolGen cp)
    Declare a field. If it is static (isStatic() == true) and has a basic type like int or String it may have an initial value associated with it as defined by setInitValue().
    Parameters:
      `accessFlags` - access qualifiers.
      `type` - field type.
      `name` - field name.
      `cp` - constant pool.

- ## Method Details

  - ### getComparator

    public static BCELComparator<FieldGen> getComparator()
    Gets the comparison strategy object.
    Returns:
      Comparison strategy object.

  - ### setComparator

    public static void setComparator(BCELComparator<FieldGen> comparator)
    Sets the comparison strategy object.
    Parameters:
      `comparator` - Comparison strategy object.

  - ### addObserver

    public void addObserver(FieldObserver o)
    Add observer for this object.
    Parameters:
      `o` - the observer to add.

  - ### cancelInitValue

    public void cancelInitValue()
    Remove any initial value.

  - ### copy

    public FieldGen copy(ConstantPoolGen cp)
    Creates a deep copy of this field.
    Parameters:
      `cp` - the constant pool.
    Returns:
      deep copy of this field.

  - ### equals

    public boolean equals(Object obj)
    Return value as defined by given BCELComparator strategy. By default two FieldGen objects are said to be equal when their names and signatures are equal.
    Overrides:
      `equals` in class `Object`
    See Also:
      - `Object.equals(Object)`

  - ### getField

    public Field getField()
    Gets field object after having set up all necessary values.
    Returns:
      the field object.

  - ### getInitValue

    public String getInitValue()
    Gets the initial value.
    Returns:
      the initial value.

  - ### getSignature

    public String getSignature()
    Description copied from class: `FieldGenOrMethodGen`
    Gets the signature of method or field.
    Specified by:
      `getSignature` in class `FieldGenOrMethodGen`
    Returns:
      signature of method/field.

  - ### hashCode

    public int hashCode()
    Return value as defined by given BCELComparator strategy. By default return the hash code of the field's name XOR signature.
    Overrides:
      `hashCode` in class `Object`
    See Also:
      - `Object.hashCode()`

  - ### removeObserver

    public void removeObserver(FieldObserver o)
    Remove observer for this object.
    Parameters:
      `o` - the observer to remove.

  - ### setInitValue

    public void setInitValue(boolean b)
    Sets the initial value.
    Parameters:
      `b` - the boolean value.

  - ### setInitValue

    public void setInitValue(byte b)
    Sets the initial value.
    Parameters:
      `b` - the byte value.

  - ### setInitValue

    public void setInitValue(char c)
    Sets the initial value.
    Parameters:
      `c` - the char value.

  - ### setInitValue

    public void setInitValue(double d)
    Sets the initial value.
    Parameters:
      `d` - the double value.

  - ### setInitValue

    public void setInitValue(float f)
    Sets the initial value.
    Parameters:
      `f` - the float value.

  - ### setInitValue

    public void setInitValue(int i)
    Sets the initial value.
    Parameters:
      `i` - the int value.

  - ### setInitValue

    public void setInitValue(long l)
    Sets the initial value.
    Parameters:
      `l` - the long value.

  - ### setInitValue

    public void setInitValue(short s)
    Sets the initial value.
    Parameters:
      `s` - the short value.

  - ### setInitValue

    public void setInitValue(String str)
    Sets (optional) initial value of field, otherwise it will be set to null/0/false by the JVM automatically.
    Parameters:
      `str` - the string value.

  - ### toString

    public final String toString()
    Return string representation close to declaration format, for example: 'public static final short MAX = 100'.
    Overrides:
      `toString` in class `Object`
    Returns:
      String representation of field.

  - ### update

    public void update()
    Call notify() method on all observers. This method is not called automatically whenever the state has changed, but has to be called by the user after they have finished editing the object.