static method using a default instance?
| 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