Static method

O

ojvm24

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 retrives
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

<code>
public static int valor;//the variable was declared as static.

public static List createBeanCollection(){
List beans = miDao.onbtenObjetosPorId(valor);//i can use it now
return beans;
}
</code>
however this solution is not the best, because i'm expossing the
properties of my class
hope you can help me with this little problem, just remember i can't
change the method
signature.

Thank you in advance.
 
A

Arne Vajhøj

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 retrives
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

<code>
public static int valor;//the variable was declared as static.

public static List createBeanCollection(){
List beans = miDao.onbtenObjetosPorId(valor);//i can use it now
return beans;
}
</code>
however this solution is not the best, because i'm expossing the
properties of my class
hope you can help me with this little problem, just remember i can't
change the method
signature.

You can make valor private and add a public setValor method.

But your basic problem is unsolvable. You can not an argument to
a method without adding an argument.

You have painted yourself into a corner with that requirement.

Arne
 
J

Joshua Cranmer

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

May I ask why you cannot change the parameters? Is it to conform to some
interface or something similar?
<code>
public static int valor;//the variable was declared as static.

public static List createBeanCollection(){
List beans = miDao.onbtenObjetosPorId(valor);//i can use it now
return beans;
}
</code>
however this solution is not the best, because i'm expossing the
properties of my class

I'm guessing that you would like something like this:

public class ProblemClass {
private static boolean validConfiguration = false;

private static int value;

public static void configureCollection(int value) {
ProblemClass.value = value;
validConfiguration = true;
}

public static List createBeanCollection() {
if (!validConfiguration)
throw new IllegalStateException("Trying to create a bean"+
" collection without a proper configuration!");
validConfiguration = false;
return miDao.onbtenObjetosPorId(value);
}
}

Disclaimers:
1. It would probably be better to use generics, unless this is pre-Java
5, in which case it would be better to upgrade to Java 6 and then use
generics.
2. I don't know Spanish, so I'm only guessing what your variable names
are talking about.
3. My code doesn't account for more complex fault-handling capabilities,
but that's alright for an example.
 
M

Mark Space

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 retrives
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

<code>
public static int valor;//the variable was declared as static.

public static List createBeanCollection(){
List beans = miDao.onbtenObjetosPorId(valor);//i can use it now
return beans;
}
</code>
however this solution is not the best, because i'm expossing the
properties of my class
hope you can help me with this little problem, just remember i can't
change the method
signature.

Thank you in advance.

Probably a better way to handle this is just to use a setter to set the
value of valor.

public class BeanWrapper {

private int valor;

public static void setValor( int v )
{
valor = v;
}

public static List createBeanCollection()
{
return miDao.onbtenObjectosPorId( valor );
}
}

Now this is a tad dangerous, you'll get issues with concurrency and
what-not. But it might suit your needs.

Have you considered a non-static method? It seems to me that would solve
a lot more problems. You could also inject a non-static object into the
wrapper class.

public class BeanWrapper {

private Beans b;

public static void setValor( Beans v )
{
b = v;
}

public static List createBeanCollection()
{
return b.onbtenObjectosPorId();
}
}

where a Beans object has the "valor" injected into it, ie.,

setValor( new Beans( 1 ) );

Now you also have a fully constructed object that you can synchronize
on, for example. And it's private, so no one else can call it but your
createBeanCollection() method.
 
O

ojvm24

May I ask why you cannot change the parameters? Is it to conform to some
interface or something similar?



I'm guessing that you would like something like this:

public class ProblemClass {
private static boolean validConfiguration = false;

private static int value;

public static void configureCollection(int value) {
ProblemClass.value = value;
validConfiguration = true;
}

public static List createBeanCollection() {
if (!validConfiguration)
throw new IllegalStateException("Trying to create a bean"+
" collection without a proper configuration!");
validConfiguration = false;
return miDao.onbtenObjetosPorId(value);
}

}

Disclaimers:
1. It would probably be better to use generics, unless this is pre-Java
5, in which case it would be better to upgrade to Java 6 and then use
generics.
2. I don't know Spanish, so I'm only guessing what your variable names
are talking about.
3. My code doesn't account for more complex fault-handling capabilities,
but that's alright for an example.

thank you for answer so soon. ok i think your solution is better than
the one i'm using now. about the use of generics mmm. i'll check it
because this is a List for ireport reporter so i don't know if it is
supported. the variable name doesn't matter, it doesn't have any
meaning. it seems to work so i'll give it a try.
 
O

ojvm24

You can make valor private and add a public setValor method.

But your basic problem is unsolvable. You can not an argument to
a method without adding an argument.

You have painted yourself into a corner with that requirement.

Arne

yes i know that it's not the best way to get something from some
method. the point is that this is a requirement of ireport in order to
populate it with a list of objects instead of using an sql data
source. so that the reazon than i can change the signature.
 
O

ojvm24

Probably a better way to handle this is just to use a setter to set the
value of valor.

public class BeanWrapper {

private int valor;

public static void setValor( int v )
{
valor = v;
}

public static List createBeanCollection()
{
return miDao.onbtenObjectosPorId( valor );
}

}

but with this code you will get an "non-static variable valor cannot
be referenced from a static context" so this wont work.
Now this is a tad dangerous, you'll get issues with concurrency and
what-not. But it might suit your needs.


yes in fact i was thinking in sincronized code in the method that
calls this class.
Have you considered a non-static method? It seems to me that would solve
a lot more problems. You could also inject a non-static object into the
wrapper class.

the problem is that this is the signature a must use. i cant rewrite
it to "public List mimethod()" this is an ibatis requirementin order
to populate a report whit objects instead of using an sql data source.
public class BeanWrapper {

private Beans b;

public static void setValor( Beans v )
{
b = v;
}

public static List createBeanCollection()
{
return b.onbtenObjectosPorId();
}

}


yes it souns fine. but the problem is the same i mentioned above.
where a Beans object has the "valor" injected into it, ie.,

setValor( new Beans( 1 ) );

Now you also have a fully constructed object that you can synchronize
on, for example. And it's private, so no one else can call it but your
createBeanCollection() method.

thank you for your time.
 
M

Mark Space

but with this code you will get an "non-static variable valor cannot
be referenced from a static context" so this wont work.


Oops, both examples should have "static" as part of the variable
declaration. That's what I get for not compiling the code.
 
D

Daniel Pitts

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 retrives
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

<code>
public static int valor;//the variable was declared as static.

public static List createBeanCollection(){
List beans = miDao.onbtenObjetosPorId(valor);//i can use it now
return beans;
}
</code>
however this solution is not the best, because i'm expossing the
properties of my class
hope you can help me with this little problem, just remember i can't
change the method
signature.

Thank you in advance.

If you need to pass something in, you should make sure its thread safe.
One way to do that is have a ThreadLocal with your parameters.
 
M

Mark Space

Daniel said:
If you need to pass something in, you should make sure its thread safe.
One way to do that is have a ThreadLocal with your parameters.

Assuming, of course, that "valor" isn't supposed to be a global shared
variable.

But yes, going from a local variable to a global one creates all sorts
of problems if you don't understand what a static field does. Good
catch there.
 
O

Owen Jacobson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top