Field declaration

Fields are data for holding the status of individual instances of the class.


Table of contents:


Format

Class name

Field name

Semicolon

Features

Example

Format

The format is:


Class-name Field-name;


This is a declaration statement.

If names are different and outside the other member definitions in the class definition, you can declare fields as many as you like and place anywhere.

Declared field can be referred in the class scope. Therefore, you can refer them in any location of the class definition.

Class name

Class-name is a class that will be bound to the field.

Rice is a strongly typed language. You can't declare fields or variables by name alone. You need a class name and identifier pair.

Field name

Field-name is an identifier that is used to access a field. The naming rules for field name is the same as for identifiers.

You should specify a unique name that does not conflict with other members. If there is a name conflict, an exception will be thrown or the name will be hidden.

Semicolon

The end of the field declaration is represented by a semicolon. The semicolon is a common statement terminator used in Rice.

Features

Fields have several features.


You can declare it, but cannot define it.

You can declare a field of unknown class.

It can only be accessed within the class definition.

Declaration and definition

Fields cannot be defined at the same time as declaring.


int intField = 10;


Definition statement like above is not allowed. The field is initialized by the fitter.

Field declaration is a mere placeholder for associating the class and name.

Declaration of unknown class

You can declare a field of unknown class.

If you declare a field of unknown class, the dummy class will be temporarily placed by the system. The dummy class is a class dedicated to temporary placing.

Even if definition of the unknown class is completed, the temporarily placed dummy class remains as it is.

That is, a class with such a field will be incomplete. You should initialize the class before using by fitter.

Access control

Members other than fields can control their access level by using the keyword, open or closed, at the beginning of the definition.

You can call open members from outside of the class definition, cannot call closed members from outside.

The field access level is fixed at closed. there is no notation for such access control. You can access the fields directly only inside of the class definition.

Example

We define the example class and the circular class.

The circular class is declared as a field at the line 4. This is the "unknown class declaration".

1:

class example

2:

int x;

3:

int y;

4:

circular cirField; // Unknown class. The dummy class is temporarily placed.

5:

open fitter example(circular cir) // Fitter.

6:

cirField = cir;

7:

endfitter

8:

endclass

9:

10:

class circular

11:

bool boolFied;

12:

endclass

You can initialize the example class to its full state by doing the following:

1:

circular cir;

2:

example ex = new example(cir); // It replaces the cirField to the circular.

Copyright © Rice All rights reserved.