4.4 Using Fields

To get the value of a field from an object, use the getfield instruction. Static fields are handled differently; they're discussed in section 4.10. The getfield instruction expects to find the object from which to get the field on top of the operand stack. The arguments to the getfield instruction are the class, name, and descriptor of the field. It is necessary to include the descriptor so that the verification algorithm can check the class without having to load other class files to determine the type of the field.

We'll use this class, as defined in Java, for our examples.

 class Greeting {    String intro = "Hello"; } 

You can get the value of the field with

 .method static useGreeting(LGreeting;)V aload_0                   ; Push the greeting in variable 0                           ; Now fetch the field getfield Greeting/intro Ljava/lang/String; ; Now the String Hello is on top of the stack 

The getfield instruction takes the Greeting object which is on top of the stack, and pushes onto the stack the value of its intro field. Diagrammed, memory looks like Figure 4.2 before the getfield instruction, like Figure 4.3 afterwards.

Figure 4.2. Before getfield

graphics/04fig02.gif

Figure 4.3. After getfield

graphics/04fig03.gif

In Figure 4.2, the top of the stack contains a reference to a Greeting object, which has a field that points to the String labeled "Hello". Local variable 0 points to the same Greeting object. The stack and the local variable array contain identical copies of the same reference. Since they are the same, they point to the same object.

When getfield is executed, the top of the stack is removed, and it is replaced with a reference to the "Hello" object. The local variable array is unchanged; only the stack has been altered.

The reference in the intro field of the Greeting object is the same as the one on top of the stack. The object itself has not been altered.

4.4.1 Types and getfield

The getfield instruction requires the type of the field that it is getting, as well as the name. This is true even if the field contains one of the numeric types. Suppose that you have a class Point:

 class Point {    float x, y; } 

To add the value of the x and y fields, you would use getfield like this:

 ; This method takes a Point and returns the sum of its x and y ; coordinates .method static xySum(LPoint;)F aload_0               ; Push the Point getfield Point/x F    ; Get its x coordinate (must be a float) aload_0               ; Push the Point again getfield Point/y F    ; Get its y coordinate (must be a float) fadd                  ; Add them together freturn               ; Return the result .end method 

The JVM uses the type information in the arguments as part of the verification process. The x and y fields are required to be floats. As long as they are, it is safe to use the fadd instruction to add them together. This means that you can check the validity of the method without having to look at the Point class itself.

The JVM must ensure that there are x and y fields in the Point class when it gets around to loading the class. Then it can check that the fields have the correct type.

4.4.2 Inheriting Fields

When one class has another class as a superclass, it inherits all of the nonstatic fields of that class. Consider an extension to the Greeting class to handle greetings in Russian:

 class RussianGreeting extends Greeting {    String intro = "Zdravstvuite"; } 

An instance of RussianGreeting has two fields, both named intro. An instance of RussianGreeting looks like Figure 4.4 . The full names of the fields are different. In Java, the English-language intro is hidden behind the Russian-language version. In Oolong, they are both equally accessible:

 .method static internationalGreetings(LRussianGreeting;)V aload_0               ; There is a RussianGreeting in register 0                       ; Push "Zdravstvuite": getfield RussianGreeting/introduction Ljava/lang/String; aload_0               ; Reload the same object                       ; Push "Hello": getfield Greeting/introduction Ljava/lang/String; ;; Rest of the method omitted .end method 
Figure 4.4. An instance of RussianGreeting

graphics/04fig04.gif

At the end of the code shown, there will be two objects on the stack. The bottom of the stack will contain a reference to the String "Zdravstvuite", and above it will be a reference to the String "Hello".

4.4.3 Changing Field Values

To get the value of a field, use getfield, which takes an object on the stack and leaves in its place the value of the field. The counterpart of getfield for changing the value of a field is the putfield instruction, which uses the same arguments as getfield but expects both the object and the field value on the stack before it starts. For example, to change the value of the intro field,

 .method static makeAustralian(LGreeting;)V aload_0               ; Push the Greeting ldc "G'Day"           ; Set up a new intro and store it putfield Greeting/intro Ljava/lang/String; return .end method 

This changes the value of the Greeting/intro field, whether the object is an ordinary Greeting or a RussianGreeting. If the argument is a RussianGreeting, its RussianGreeting/intro field is unchanged. Thus, this Java code:

 Greeting g = new RussianGreeting(); makeAustralian(g); System.out.println(g.intro); System.out.println(((RussianGreeting) g).intro); 

prints

 G'Day Zdravstvuite 

Exercise 4.1

Write an Oolong declaration for the class Dinosaur, which has a name and a field indicating whether or not it is carnivorous. Include a constructor that initializes both fields.

Exercise 4.2

Write Oolong code that creates a Velociraptor, which is a carnivorous dinosaur. Don't forget to initialize it.

Exercise 4.3

Write an Oolong declaration for CarnivorousDinosaur, which is a subclass of Dinosaur that is carnivorous. Be sure to include a constructor to give the name. How would you create a Velociraptor using this class?



Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net