log.info "starting" // we use class to create objects of a class Planet p1 = new Planet() Planet p2 = new Planet() //Planet.name = "Pluto" illegal Planet.shape = "Circle" // static variable Planet.log = log p1.name = "earth" // non static variable p2.name = "jupiter" p1.printName() // non static has to be called with reference Planet.revolve() // static can be called with class class Planet{ // variables and functions def name // non static variable def static shape // static variable def static log public void printName(){ // non static function log.info ("Name of planet is $name. Shape is $shape") xyz() // non static can access static } public static void revolve(){ // static function //log.info (name) // error, static cannot access non static xyz() // call one function from another function log.info ("Planet revolving. Shape is $shape") } public static void xyz(){ log.info "inside xyz" } }
Test Result:
Tue Oct 06 18:30:29 CST 2015:INFO:starting Tue Oct 06 18:30:29 CST 2015:INFO:Name of planet is earth. Shape is Circle Tue Oct 06 18:30:29 CST 2015:INFO:inside xyz Tue Oct 06 18:30:29 CST 2015:INFO:inside xyz Tue Oct 06 18:30:29 CST 2015:INFO:Planet revolving. Shape is Circle
Note :
Static cannot access non static