Scope of execution of variables

 

Here is the code used in this lesson:


//5) VARIABLES DEFINED INSIDE A CONTROL STRUCTURE

RuleContext ruleContextVars {
    Rule rule {
        if (true) {
            String a = "hello"
        }
        
        a = "false" // error, it can not be accessed since the scope of the variable ended in the control structure
        
        if (true) {
            String a = "bye"
            
            if (false) {
                a = "hello" // correct
            }
        }
        
        Integer strVar
        
        strVar = 5 // correct
        
        if (true) {
            Bool strVar
            
            strVar = true // correct
        }

    }
}

> Next