Groovy web console

subscribe to the feed Subscribe
to this
site
Accessing "class methods" outside class instance on Groovy (via #groovywebconsole)
tweet this snippet Tweet
this
script

Accessing "class methods" outside class instance on Groovy

Published 2 months ago by Brian Bohe with tags Groovy ClassMethods Static
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
public class A{
  static int number = 0;
  def static B(){
      number+=1
      number
  }
}


def a = new A()
for(int i =0; i < 5; i ++){
    println "call to static method from an instance return ${a.B()}".toString()
    println "call to static var from an instance return ${a.number}".toString()
    println "call to static method from class return ${A.B()}".toString()
    println "call to static var from class return ${A.number}".toString()
}

println A.getClass()
println a.getClass()