putfield 0xb5

Pops an object and a value; stores the value in the field.

Opcode
0xb5 (181)
Family
Fields
Operands
  • 2 bytes: constant pool index
Length
3 bytes
Operand stack
…, objectref, value → …
Can throw
NullPointerException

What it does

Pops an object reference and a value, and stores the value in an instance field. A constructor usually starts with putfields for each field initialiser, after the call to the superclass constructor.

A final instance field may only be written in a constructor of its own class.

How often javac uses it

In JDK 25.0.5's java.base module, putfield appears 24,204 times in 3,695 of its 7,391 classes: 1.2% of all instructions, the 24th most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedInputStream.<init>, java.io.BufferedOutputStream.<init>, java.io.BufferedReader.<init>, java.io.BufferedReader$1.<init>.

Examples

From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; putfield is highlighted.

int assignField(int v) in Tour, compiled by javac 25
74return count = v;
0aload_0 // this
1iload_1 // v
3putfield #31// Tour.count : int

Open in the viewer

Bridge(int rank) in Bridge, compiled by javac 25
8Bridge(int rank) {
0aload_0 // this
1invokespecial #1// new Object()
9this.rank = rank;
4aload_0 // this
5iload_1 // rank
6putfield #7// Bridge.rank : int
10}

Open in the viewer

Related instructions

getstatic Pushes the value of a static field.putstatic Pops a value into a static field.getfield Pops an object; pushes the value of one of its fields.

Specification: JVMS §6.5, putfield (Java SE 25).