class - Grails initialization of classes -
i have question initialization wiith groovy/grails. when have following class, sinstance doesn't passed sservice initialization.
class { string sinstance string app string dbinstance sservice s = new sservice(sinstance:sinstance, app:app) }
sservice class:
class sservice { string sinstance string app public getsinstance{ return sinstance } }
this returns null, where
class { string sinstance string app string dbinstance public initializesservice{ sservice s = new sservice(sinstance:sinstance, app:app) } }
returns sinstance variable sservice class.
why , how can have sservice object initialized class constructor?
you can't this:
class { string sinstance string app string dbinstance sservice s = new sservice(sinstance:sinstance, app:app) }
the problem when creating instance of sservice, sinstance has not been initialized yet. if want pass sinstance constructor of other class within class, going have after sinstance has been assigned value, method called after constructed.
edit:
trying clarify comments below:
class { string sinstance string app string dbinstance void anymethod() { // work long have initialized sinstance sservice s = new sservice(sinstance:sinstance, app:app) } }
depending on trying do, maybe in direction:
class { string sinstance string app string dbinstance sservice s void initializes() { if(s == null) { // work long have initialized sinstance s = new sservice(sinstance:sinstance, app:app) } } }
or:
class { string sinstance string app string dbinstance sservice theservice sservice gettheservice() { if(theservice == null) { // work long have initialized sinstance theservice = new sservice(sinstance:sinstance, app:app) } theservice } def somemethodwhichusestheservice() { gettheservice().dosomethingtoit() } }
Comments
Post a Comment