10.14 Field Declarations

Here are some examples of field declarations:

 long number; public int x; protected volatile String name = "Nobody"; static private Employee[] employee_list; 

The basic pattern is that it begins with a (possibly empty) string of modifiers (such as private, public, static, and so on). The modifiers are followed by the type of the field (such as int, String, or Employee[]). Next comes the name of the field. Finally, there may be an initializer, indicated by an = followed by an expression. As with other assignments, the compile-time type of the expression type must conform to the declared type of the field. The Java compiler imposes the restriction that a class may have only one field with a particular name, even if the fields have different types. The JVM does not have this restriction, but if you intend your JVM classes to be used with Java code, then you should observe the restriction anyway.

Compiling field declarations is just a matter of converting the type expression into the appropriate JVM equivalent:

 .field y I .field public x I .field protected volatile name Ljava/lang/String; .field static private employee_list [LEmployee; 

The modifiers are left as they are. In the class file that results, they are used to turn on bits in the FieldInfo structure of the class file. The Java virtual machine interprets those bits to mean what the equivalent modifier keywords are supposed to mean in Java.

The initializers do not appear in the .field directive unless the field is both static and final and the initializer is one of the basic numeric types. They translate into code in the body of the constructor, as discussed in section 10.16.3.

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