prev next contents
getfield

get value of object field

Jasmin Syntax


    getfield <field-spec> <descriptor>
e.g.
    getfield java/lang/System/out Ljava/io/PrintStream;
<field-spec> is composed of two parts, a classname and a fieldname. The classname is all of the characters in the <field-spec> up to the last '/' character, and the fieldname is the rest of the characters after the last '/'. For example:

    foo/baz/AnotherClass/aVeryLongFieldName
    -- classname -------/-- fieldname -----

{{JM - can we turn this into a nicer diagram?}}


<descriptor> is the Java type descriptor for the field, for example Ljava/io/PrintStream;

In Jasmin, the .field directive is used to add a field to a class. See Chapter 16 for a description of this and other Jasmin directives.

Stack

Before

After
objectref
value
...
...
or, for fields that hold doubles or longs:

Before

After
objectref
value-word1
...
value-word2

...
Description

getfield pops objectref (a reference to an object) from the stack, retrieves the value of the field identified by <field-spec> from objectref, and pushes the one-word or two-word value onto the operand stack.

For example, if you have the class:


    package xyz;
    class Point {
        public int xCoord, yCoord;
    };

Then, assuming p is an instance of the class Point, writing the Java expression:

    int x = p.xCoord;
generates a call to getfield like:

    aload_1                      ; push object in local varable 1 (i.e. p) onto the stack
    getfield xyz/Point/xCoord I  ; get the value of p.xCoord, which is an int
    istore_2                     ; store the int value in local variable 2 (x)
In Jasmin, getfield takes two parameters, <field-spec> and <descriptor>. <field-spec> gives classname, the name of the class that defines the field, as well as fieldname, as the name of the field itself. In the example above, the <field-spec> is "xyz/Point/xCoord", indicating that the classname is "xyz/Point" and the fieldname is "xCoord". <descriptor> describes the type of data held in the field, and is a standard Java type descriptor (see Chapter 4 for a full description of type descriptors). In the example above, <descriptor> is "I", since the field holds an integer.

getfield first resolves classname to a Java class. Then it locates the fieldname in that class, determining the width of the field (in bytes) and its offset (in bytes) from the base of the object data. The type of the field must match <descriptor>. See Chapter 7 for more on how fields are resolved

To retrieve the value for the field, getfield obtains the bytes starting at offset and extending for width bytes from the start of objectref's instance data, expands it to either a 4-byte or 8-byte value, and pushes the value onto the operand stack.

Exceptions

NullPointerException - objectref is null

Bytecode

In bytecode, the getstatic opcode is followed by a 16-bit unsigned integer index. This is the index of an entry in the constant pool of the current class. The entry is tagged a CONSTANT_Fieldref entry. It indicates a CONSTANT_Class entry that gives the name of the class containing the field (i.e. classname), and a CONSTANT_NameAndType entry that gives the fieldname, and the type <descriptor>.

Type

Description
u1
getfield opcode = 0xB4 (180)
u2
index
See Also

putfield, putstatic, getstatic

Notes

Fields cannot be overriden, although they can be 'shadowed'. For example, with the two classes:


    class A { int x; }
and
    class B extends A { int x; }
instances of B will have storage for both the field "A/x" and the field "B/x". Which field is accessed is determined by the class name given in <field-spec>.


prev next contents
Java Virtual Machine, by Jon Meyer and Troy Downing, O'Reilly Associates