---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 12 Edition"
java_version: 12
chapter: "Chapter 4. The class File Format"
section: "4.10.1.9.putfield"
heading: "putfield"
parent: "4.10.1.9. Type Checking Instructions"
source: https://docs.oracle.com/javase/specs/jvms/se12/html/jvms-4.html#jvms-4.10.1.9.putfield
id: "4.10.1.9.putfield"
url: https://jvm.obfuscated.software/jvms/se12/4.10.1.9.putfield.html
raw: https://jvm.obfuscated.software/jvms/se12/4.10.1.9.putfield.md
---

# putfield

A *putfield* instruction with operand `CP` is type safe iff all of the following are true:

- Its first operand, `CP`, refers to a constant pool entry denoting a field whose declared type is `FieldType`, declared in a class `FieldClassName`. `FieldClassName` must not be an array type.

- Either:

  - One can validly pop types matching `FieldType` and `FieldClassName` off the incoming operand stack yielding the outgoing type state.
  - `protected` fields are subject to additional checks ([§4.10.1.8](4.10.1.8.md)).

```
instructionIsTypeSafe(putfield(CP), Environment, _Offset, StackFrame,
                      NextStackFrame, ExceptionStackFrame) :-
    CP = field(FieldClassName, FieldName, FieldDescriptor),
    parseFieldDescriptor(FieldDescriptor, FieldType),
    canPop(StackFrame, [FieldType], PoppedFrame),
    passesProtectedCheck(Environment, FieldClassName, FieldName,
                         FieldDescriptor, PoppedFrame),
    currentClassLoader(Environment, CurrentLoader),
    canPop(StackFrame, [FieldType, class(FieldClassName, CurrentLoader)],
           NextStackFrame),
    exceptionStackFrame(StackFrame, ExceptionStackFrame).
```

- Or:

  - If the instruction occurs in an instance initialization method of the class `FieldClassName`, then one can validly pop types matching `FieldType` and `uninitializedThis` off the incoming operand stack yielding the outgoing type state. This allows instance fields of `this` that are declared in the current class to be assigned prior to complete initialization of `this`.

```
instructionIsTypeSafe(putfield(CP), Environment, _Offset, StackFrame,
                      NextStackFrame, ExceptionStackFrame) :-
    CP = field(FieldClassName, _FieldName, FieldDescriptor),
    parseFieldDescriptor(FieldDescriptor, FieldType),
    Environment = environment(CurrentClass, CurrentMethod, _, _, _, _),
    CurrentClass = class(FieldClassName, _),
    isInit(CurrentMethod),
    canPop(StackFrame, [FieldType, uninitializedThis], NextStackFrame),
    exceptionStackFrame(StackFrame, ExceptionStackFrame).
```
