static method using a default instance?

"[email protected]" <[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <[email protected]>
Hi there,

I've bumped into a pretty trivial problem, but probably I've got a dumb day today — can't find a solution :(

How does one write a method which can be called either over an instance, or over a class, in which case it would automatically use a default instance? Like this, in pseudo-code which does not work in Groovy/Java, but shows pretty well what I need:

===
class Foo {
  static defaultInstance=new Foo()
  def foo() { ... does its stuff on this ... }
  static foo() { defaultInstance.foo() }
}
===
// usage when I happen to have the instance:
Foo o= ...
o.foo() // does its stuff on o
// usage otherwise
Foo.foo() // does its stuff on the default instance
===

Thanks!
OC
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.