Hiding

Hiding of identifier

If ascope surrounds another scope, and an identifier of the same name has been declared at the both scope, an identifier that is declared in the inner will be used Preferentially. Namely, an identifier that is declared at the inner will hide an identifier that have same name of the outer.

If an identifier has been hidden, a special notation to refer a hidden identifier is not.


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 : Inner flag is used. flag2 is false.


A outer variable of the same name can be used until you declare a variable of the same name at the inner.


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 : Outer flag is used. flag2 is true.

♦2 : It is hidden from here.

♦3 : Inner 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 : Inner flag will be defined by outer flag. The outer flag will be hidden after that.

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 will occur.

If a variable name matches to a field, setter, or getter name. you cannot access it within 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. The hiding of identifier take place in this order.


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

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

Copyright © Rice All rights reserved.