Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  Fields
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

A field declaration is the following format.


Class-name Field-name;


This is a declaration statement ending with a semicolon after specifying the class name and field name.

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 an identifier that will be bound to the field.

Rice is a strongly typed language, so you can't declare fields or variables by name alone. You will 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.

For the field name, 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 you 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 described later.

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

Declaration of unknown class

You can declare a field even if its class is not defined before the declaration.

If you declare a field before class definition, the dummy class will be temporarily placed. The dummy class is a class dedicated to temporary placing.

Even if the class definition 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.

Access control

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

Open members can be called from outside of the class definition. Closed members cannot be called from outside of the class definition.

Since 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

As an example, 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. We will explain later.

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 to the circular class.

Next
Previous
Copyright © Rice All rights reserved.