Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  fields

Purpose of this chapter.

We will learn about the field which is the basic elements that make up the class definition.


The Field is data for holding the status of individual instances of the class.

First, let us look at the field declaration part of Circle.88.


// -----------------------------------------------Fields


int _x; // x-coordinate of the center point. The range is from 11 to 589.


int _y; // y-coordinate of the center point. The range is from 11 to 589.


int _radius;


document _doc;


window _win;


element _box; //div element. A container of canvas element.


bool _fitted; // Whether it has been initialized.


int _hor; //Horizontal movement direction and amount. plus value, it will move right. minus value, it will move left.


int _ver; //Vertical movement direction and amount. plus value, it will move down. minus value, it will move up.


int _top; // It is top of the _box in pixels.


int _left; // It is left of the _box in pixels.


timer _tt; // A timer for moving.

A declaration of the field is the following format.

class-name field-name ;

This is a statement that ends with a semicolon after the two identifier. The first identifier is a class name. The second is a field name.

The field can be declared any number in any place. However, The same name field cannot be declared.

Declared field can be referred using the field name in any location of the class definition.

Class name.

Class name represents the type of an instance that will be bound to the field.

Field name.

Field name is an identifier that is used to access the field.

The end of the field declaration.

The end of the field declaration is represented by a semicolon.

features of the field declaration.

The field of Rice has some features.

The declaration is allowed. However, to define at the same time is not allowed.

The declaration of unknown class is allowed.

There is no way to refer directly from outside the class definition.

Declaration and definition

The Field of the Rice cannot be defined at the same time as declaring.


int _iField = 10;

As described above, such notation (definition) is not allowed. Whether the field has been properly initialized is the responsibility of the fitter.

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

Declaration of unknown class

If the definition of the class of the field has been finished before the field declaration, an instance of its class is stored as the field.

However, there is a case that cannot define the class of the field before the field declaration.


// Definition of class A

class A

B _b; // Field declaration. The class B is a unknown class.


// fitter

open fitter A(B b)

_b = b;





// Definition of class B

class B

A _a; // Field declaration. The class A is a known class.



As above, it must declare a field of unknown class in the case of a class that refer to each other.

In such a case, Rice will go on defining the class by temporarily storing class (class dummy). Not surprisingly, such class is an incomplete.

To properly initialize the incomplete class is the responsibility of the fitter.

In above example, using the fitter of the class A,

B bInstance;

A aInstance = new A(bInstance);

Instances that refer to each other will be initialized properly by this way.

Access control

Members other than the field can control the level of access by using the appropriate keyword (open or closed) at the beginning of the definition.

Field does not have a notation for such access control. Directly access to the field using the field name is allowed only the inside of the class definition.

Field name is not to play the role of the interface of the class. If access to the field requires from the outside of class definition, please define a setter or a getter.

The field of all instances of the same class can be accessed using the field name at the inside the class definition.

Copyright © Rice All rights reserved.