4.10 Static Fields and Methods

Fields and methods marked static are different from those not so marked. Static fields belong to the class as a whole, rather than having a slot in each object. Since a class may be loaded into the JVM only once, there is exactly one copy of a static field in the entire virtual machine. This makes static fields convenient for storing "absolute truths" of the system, like constants. They're also handy for storing a piece of system state shared by every object, such as a counter.

Static methods are the counterpart of static fields. They're invoked not on any particular object; they're just invoked. For that reason, local variable 0 is not reserved for the this object, as it is in other method invocations. Instead, the arguments start at 0.

To invoke a static method, use the invokestatic instruction. It's used exactly like invokespecial or invokevirtual, except that only the arguments appear on the stack. Because there is no object to use for computing virtual dispatch, the exact method named in the invokestatic instruction is invoked.

Similarly, getstatic and putstatic are used on static fields instead of getfield and putfield. For example,

 .class Counter .field static private nextValue I = 0 .method static getNext ()I .limit stack 3 getstatic Counter/nextValue I   ; Push the value dup                             ; Copy it iconst_1 iadd                            ; Add 1 to the copy, which yields                                 ; the next value putstatic Counter/nextValue I   ; Store the incremented copy ireturn                         ; Return the original value .end method 

Although static fields and methods are accessed without respect to a particular object, they're still considered part of the class for purposes of figuring out which methods are permitted to access which fields and methods. The definition of private states that a field or method may be accessed from any method in the same class, even a static one.

For example, consider this part of an implementation of class Complex, for complex numbers. It uses a static method add, which adds two Complex numbers to produce a new Complex. Because it is part of the class, it is allowed to access the private real and imaginary fields, which are not accessible from outside the class.

 .class Complex .field private real F          ; Each complex number has a real .field private imaginary F     ; and imaginary component ; The add method, though static, has access to the private state ; of its arguments .method public static add(LComplex;LComplex;)LComplex; .limit stack 5 .limit locals 3 new Complex                ; Create a new complex number dup aload_0                    ; Get the real value of the first arg getfield Complex/real F aload_1                    ; Get the real value of the second arg getfield Complex/real F fadd                       ; Add them aload_0                    ; Do the same for the imaginary parts getfield Complex/imaginary F aload_1 getfield Complex/imaginary F fadd                       ; Initialize the object invokespecial Complex/<init>(FF)V areturn                    ; And return it .end method 


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

Similar book on Amazon

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