putfield

Set field in object

Bytecode

Type Description
u1 putfield opcode = 0xB5 (181)
u2 index

Stack ..., objectref, value => ... OR  ..., objectref, value.word1, value.word2 => ...

Description

The unsigned index is used to get access to field entry, a reference to a class name and a field name. If the field is protected, then it must be either a member of the current class or a member of a superclass of the current class, and the class of objectref must be either the current class or a subclass of the current class.

The constant pool item is resolved, determining both the field width and the field offset. The type of a value stored by a putfield instruction must be compatible with the descriptor of the field  of the class instance being stored into. If the field descriptor type is byte, char, short, or int, then the value must be an int. If the field descriptor type is float, long, or double, then the value must be a float, long, or double, respectively. If the field descriptor type is a reference type, then the value must be of a type that is assignment compatible with the field descriptor type.

The value and objectref, which must be of type reference, are popped from the operand stack. The field at the offset from the start of the object referenced by objectref is set to the value.

Exception

NullPointerException - objectref is null

Notes

The putfield instruction operates on both one- and two-word wide fields.