Home About Rice Downloads Documents Others Cooker CookerGX
Documents  >  Reference  >  hiding
hiding

hiding of identifier

If scope surrounds the other scope, and an identifier of the same name has been declared at the both scope , an identifier that is declared in the inside will be used Preferentially at the internal scope. Namely, an identifier that is declared at the inside will hide an identifier that have same name of the external.

If an identifier has been hidden, a special notation to refer a hidden identifier is not. Avoiding the hiding is the responsibility of the programmer.

For example,


class sample

open method void sample_method()

bool flag = true;

if(true)

bool flag = false; //♦1

bool flag2 = flag; //♦2

endif

endmethod

endclass


♦1 : Defines a variable of the same name.

♦2 : Inside flag is used. flag2 is false.


The external variable of the same name can be used until you declare the variable of the same name at the inside.


class sample

open method void sample_method()

bool flag = true;

if(true)

bool flag2 = flag; //♦1

bool flag = false; //♦2

flag2 = flag; //♦3

endif

endmethod

endclass


♦1 : Outside flag is used. flag2 is true.

♦2 : It is hidden from here.

♦3 : Inside flag is used. flag2 is false.


Since the definition statements are evaluated from the right side.


class sample

open method void sample_method()

bool flag = true;

if(true)

bool flag = flag; //♦1

endif

endmethod

endclass


♦1 : The inside flag was defined by the outside flag. It is hidden from here.

priority of reference

Hiding of the identifier is not limited to the variable name. If there is a same identifier among the elements in the class definition, the hiding of the identifier will take place.

For example, if a variable name matches to a field, setter, or getter name. you cannot access a field, setter, or getter by the name at the scope of that variable.

The order of reference is a variable in the inside scope, a variable in the outside scope, field, setter and getter. Namely, the hiding of identifier take place in this order.

It is possible to use the this operator in order to distinguish between variables and members of class.

The this operator will limit the search of the name to the class members.

But, there is a possibility that the identifier is hidden among the class members even if you use the this operator.

Previous
Copyright © Rice All rights reserved.