Fields

2.3.2 Field Declarations Variables of a class are introduced by field declarations. FieldDeclarations: FieldModifiers (opt) Type VariableDeclarators VariableDeclarator: VariableDeclaratorID = VariableInitializer VariableInitializer: Expression ArrayInitializer VariableDeclaratorID: Identifier VariableDeclaratorID[ ]

It is a compile-time error for a body of a class declaration to declare two fields of the same name. Methods, types and fields may have the same name since they are using different contexts. If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in the super classes. A class inherits from its direct super class all the non-private fields of the super class. A sub class may access a private field member in a super class but will not inherit it. Sub classes inherit all other fields from super classes. 2.3.2.1 Field modifiers Field modifiers are always one of the following. Public Protected Private Static Final Transient Volatile A compile-time error occurs if the same modifier appears more than once in a field declaration, or a field declaration has more than one of the access modifiers, public, protected and private. If two or more distinct field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above. If a field is declared static, there exists exactly only one instance of the field, no matter how many instances of the class may eventually be created. A static field is instantiated when the class is initialized. A field that is not declared static is called an instance variable which means that whenever a new instance of a class is created, a new variable associated with that instance is created for every instance variable declared in that class or any of its super classes. A field can be declared final. Both static and non-static fields may be declared final. It is a compile-time error if a blank final static variable is not definitely assigned by a static intializer of the class in which it is declared. A blank final non-static variable must definitely be assigned at the end of every constructor of the class in which it is declared, saving which a compile-time error occurs. Variables may be marked transient to indicate that they are not part of a persistent state of an object. Like in Java, Indus allows threads that access shared variables to keep private working copies of the variables. This allows for an efficient implementation of shared variables. A field may be declared volatile in which case the thread must reconcile its working copy of the field with the master copy every time it is accesses the variable. 2.3.2.2 Field initializers If a field declarator contains a variable initializer, then it has the semantics of an assignment to the declared variable. If the declarator is for a static field, then the variable initializer is evaluated and the assignment performed exactly once, when the class is initialized, else if the declarator is for a non-static field, then the variable initializer is evaluated and the assignment performed each time an instance of the class is created.

In the case of both static and non-static variables, a class that assigns a value to them, when the variables are actually declared in a direct or indirect super class, hides any previous assignment made to these variables in any of its super classes. Initialization expressions for non-static variables are permitted to refer to the current object this and to use the keyword super. This is not true of static variables. The declaration of a member variable always needs to appear before it is used.