Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Static method

Reply
Thread Tools

Static method

 
 
ojvm24@gmail.com
Guest
Posts: n/a
 
      05-03-2008
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.
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      05-03-2008
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 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
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      05-03-2008
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


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.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      05-03-2008
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 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.
 
Reply With Quote
 
ojvm24@gmail.com
Guest
Posts: n/a
 
      05-04-2008
On 3 mayo, 15:12, Joshua Cranmer <Pidgeo...@verizon.invalid> 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

>
> 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.
>
> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth


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.
 
Reply With Quote
 
ojvm24@gmail.com
Guest
Posts: n/a
 
      05-04-2008
On 3 mayo, 15:11, Arne Vajhøj <a...@vajhoej.dk> 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 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


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.
 
Reply With Quote
 
ojvm24@gmail.com
Guest
Posts: n/a
 
      05-04-2008
>
> 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.

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      05-04-2008
wrote:
>> 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.



Oops, both examples should have "static" as part of the variable
declaration. That's what I get for not compiling the code.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-04-2008
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 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.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      05-05-2008
Daniel Pitts wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static vs. non-static method Ryan Java 3 11-17-2004 02:23 PM
newbie Java question - non-static method cannot be referenced from a static context G. Burton Java 2 02-21-2004 07:45 PM
Question: non-static method dummy(int) cannot be referenced from a static context Michael Java 3 01-27-2004 08:45 PM
Please help: Non-static method wait(long) cannot be referenced from static context Gary Java 1 01-16-2004 11:35 PM
Why do static and non-static method names collide? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 12 07-05-2003 04:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57