groovyexpandometaclass

using ExpandoMetaclass in groovy print result and null value also


the sample progame when i try to run using the expandometaclass technique it give me two output one the desired result second one "null" as output, from where null is picked up ?

class testA {

    static  def X(def var) {
        Y(var)
    }

    static def Y(def var) {
        println var
    }
}
testA.metaClass.static.newMethod = {z_var -> X(z_var) }
println testA.newMethod("anish")

output:

anish
**null**

why this progranme also print null as output


Solution

  • The null is the return value from newMethod. In case you don't want this to be printed remove the println from your line

    println testA.newMethod("anish")