Java Reference
In-Depth Information
Static Fields
A class's static fields are present in every instance of the class. Space for such
fields is provisionedwhen the class is loaded and initialized. Thereafter, a static
fieldcanbeloadedorstoredbythegetstatic and putstatic instructions,
respectively.
The action of getstatic is similar to the various load instructions (iload,
aload, etc.) in that the result of the fetch is pushed on TOS. The form of a
getfield instruction as coded in Jasmin is follows:
getstatic name type
where name is the name of the static field, prefaced by its fully qualified class
name, and type is the expected type of the result.
getstatic
Many Java programs reference System.out for con-
sole output. Such accesses are actually references to
the static field java.lang.System.out,whosetypeis
java.io.PrintStream.
The example shows that after the getfield instruction
executes, the stack has a new element at TOS. The
ยท
on
the stack represents the reference to System.out.
Before
After
The JVM instruction generated for that static field above is therefore
getstatic java/lang/System/out Ljava/io/PrintStream;
The actual representation of the instruction in the JVM occupies only 3 bytes:
one specifies the getstatic opcode and the other two form a 16-bit integer
specifying a constant-pool entry. Recall that constant-pool entries are denoted
ordinally (0, 1, 2, etc.) and not by their byte-o
set in the constant pool. In this
case, the designated constant-pool entry contains the name and type operand
values for the getfield instruction.
ff
Static variables can be modified by the putstatic instruction, which pops
a value from TOS and stores the value at the location specified immediately
by the instruction.
Instance Fields
A class can declare instance fields for which instance-specific storage is allo-
cated. Every instance of type t is provided with storage for t 's instance fields.
To access those fields, a particular instance of t must be provided.
 
Search WWH ::




Custom Search