[BAEL-2665] Groovy Scopes (#9343)
* [BAEL-2665] Code example and HTTPS setup * [BAEL-2665] Spock test for one big class * [BAEL-2665] All Sub-scope tests * [BAEL-2665] Tests for local variables * [BAEL-2665] Uses logs instead of print * [BAEL-2665] Review updates * [BAEL-2665] removes unit tests * [BAEL-2665] returns value of variable in function
This commit is contained in:
parent
6800a0e43f
commit
dc24e71faf
@ -105,7 +105,7 @@
|
|||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
<id>central</id>
|
<id>central</id>
|
||||||
<url>http://jcenter.bintray.com</url>
|
<url>https://jcenter.bintray.com</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.scopes
|
||||||
|
|
||||||
|
import java.util.logging.Logger
|
||||||
|
|
||||||
|
x = 200
|
||||||
|
logger = Logger.getLogger("Scopes.groovy")
|
||||||
|
|
||||||
|
def getGlobalResult() {
|
||||||
|
logger.info(x.toString())
|
||||||
|
return 1 + x
|
||||||
|
}
|
||||||
|
|
||||||
|
def defineGlobalVariable() {
|
||||||
|
z = 234
|
||||||
|
logger = Logger.getLogger("Scopes.groovy")
|
||||||
|
logger.info(z.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("- Global variable")
|
||||||
|
logger.info(x.toString())
|
||||||
|
logger.info("- Access global variable from inside function")
|
||||||
|
logger.info(getGlobalResult().toString())
|
||||||
|
logger.info("- function called to create variable")
|
||||||
|
defineGlobalVariable()
|
||||||
|
logger.info("- Variable created inside a function")
|
||||||
|
logger.info(z.toString())
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.scopes
|
||||||
|
|
||||||
|
import java.util.logging.Logger
|
||||||
|
|
||||||
|
logger = Logger.getLogger("ScopesFail.groovy")
|
||||||
|
|
||||||
|
y = 2
|
||||||
|
|
||||||
|
def fLocal() {
|
||||||
|
def q = 333
|
||||||
|
println(q)
|
||||||
|
q
|
||||||
|
}
|
||||||
|
|
||||||
|
fLocal()
|
||||||
|
|
||||||
|
logger.info("- Value of the created variable")
|
||||||
|
logger.info(fLocal())
|
||||||
|
logger.info("- Local variable doesn't exist outside")
|
||||||
|
logger.info(q.toString())
|
||||||
|
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.scopes
|
||||||
|
|
||||||
|
import java.util.logging.Logger
|
||||||
|
|
||||||
|
logger = Logger.getLogger("ScopesFailNoPrint.groovy")
|
||||||
|
|
||||||
|
y = 2
|
||||||
|
|
||||||
|
def fLocal() {
|
||||||
|
def q = 333
|
||||||
|
println(q)
|
||||||
|
q
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(y.toString())
|
Loading…
x
Reference in New Issue
Block a user