Static method

Discussion in 'Java' started by ojvm24@gmail.com, May 3, 2008.

  1. Guest

    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.
     
    , May 3, 2008
    #1
    1. Advertising

  2. Arne Vajhøj Guest

    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
     
    Arne Vajhøj, May 3, 2008
    #2
    1. Advertising

  3. 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
     
    Joshua Cranmer, May 3, 2008
    #3
  4. Mark Space Guest

    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.
     
    Mark Space, May 3, 2008
    #4
  5. Guest

    On 3 mayo, 15:12, Joshua Cranmer <> wrote:
    > 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.
     
    , May 4, 2008
    #5
  6. Guest

    On 3 mayo, 15:11, Arne Vajhøj <> wrote:
    > 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.
     
    , May 4, 2008
    #6
  7. Guest

    >
    > 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.
     
    , May 4, 2008
    #7
  8. Mark Space Guest

    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.
     
    Mark Space, May 4, 2008
    #8
  9. Daniel Pitts Guest

    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/>
     
    Daniel Pitts, May 5, 2008
    #9
  10. Mark Space Guest

    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.
     
    Mark Space, May 5, 2008
    #10
  11. On May 4, 6:49 pm, Logan Shaw <> wrote:
    > 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

    >
    > 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
     
    Owen Jacobson, May 5, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,647
    cgbusch
    Jul 5, 2003
  2. Gary
    Replies:
    1
    Views:
    3,991
    Ryan Stewart
    Jan 16, 2004
  3. Michael
    Replies:
    3
    Views:
    4,942
    Adam Jenkins
    Jan 27, 2004
  4. G. Burton
    Replies:
    2
    Views:
    59,898
    Sudsy
    Feb 21, 2004
  5. Ryan
    Replies:
    3
    Views:
    19,555
    dingo
    Nov 17, 2004
Loading...

Share This Page