Re: Static method
On May 4, 6:49 pm, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
ojv...@gmail.com wrote:
I have a static method which has the following signature.
<code>
public static List createBeanCollection(){
}
</code>
I can't chage this, now this is the problem. how can i pass it
parameters
so in the method i can use them and return a dinamic list of objects,
i'm using ibatis
for test purposes i did the following.
<code>
public static List createBeanCollection(){
MiBean m = new MiBean();
m.setNombre("prueba");
List beans = new ArrayList();
beans.add(m);
return beans;
}
</code>
ok, it works fine, but now i want to replace the list i created by
hand with an
object that makes a query over a db, so the code would be this.
<code>
public static List createBeanCollection(){
List beans = miDao.onbtenObjetosPorId(valor);//dao that retrive=
s
object from a db.
return beans;
}
</code>
but i cant use the "valor" parameter, due is not possible to use a no=
static-variable
in a static method. at the moment i've resolved it in this way
This is an utter hack, but I suppose you're forced into creating some
sort of hack. Anyway, perhaps you could solve this problem by using
java.lang.reflect.Proxy to dynanically create a new Class object that
implements the required interface. For every different value of 'valor'=
(your variable that you are being forced to make static), you'd have
a different class. It's uuuuuuugly, but it gets around the limitation
of threads or re-entrant code stomping on the static variable.
I'm assuming here, by the way, that Proxy can implement static methods,
which seems logical, but I don't think I've tried that.
- Logan
Given that Proxy objects only expose methods defined by interfaces or
by Object, and given that interfaces cannot define static methods, I
wouldn't expect this to work.
The OP's real problem is that he's chosen to use a static method for
his lookups. His requirements (being able to change the
implementation independently of other parts of the code) do not work
well as a static method; instead, he should create an interface (or at
least a class) and plug in different implementations at runtime.
-o