I realize that Java doesn't literally allow you to pass functions as arguments. And I've seen threads that address this topic with regular members, but I can't seem to adapt it for static methods.

I have two classes that act kind of like two different "datasources" (A and B). They each have a static method that amounts to "getData" that returns an array of strings. I have no need to instantiate these classes -- that would only complicate things since they need to be accessed universally. Their interfaces are all static members.

Now I have another class (C) that needs to use this data, and C IS instantiated. I don't want to just send the data once, which would represent a static transaction, I want to send the data source object itself (ie A and B) so the data can always be fresh when the getData() method is used inside C.

I imagine I need to create some kind of superclass for A and B and pass that around. But this static method deal is really tripping me up. Any ideas of how I can modify this system?

Recommended Answers

All 3 Replies

Maybe I don't understand your problem... but public static methods on public Classes can be referenced from anywhere; you dont need to pass anything. In C you can just say A.getData(); or B.getData(); whenever you want.

Maybe I don't understand your problem... but public static methods on public Classes can be referenced from anywhere; you dont need to pass anything. In C you can just say A.getData(); or B.getData(); whenever you want.

Right, but I want to be able to use either of them in the same function within C. Kind of generically do you see? Rather than duplicating the same function twice, one using one data source, and the other using the other. Specifically the C will be instantiated many times. It has a combobox that needs to be loaded with fresh data each time it is created/displayed. For example:

public class A{
     String[] somedata;
     public static String[] getData(){
         return somedata;
     }
}
public class B{
     String[] somedifferentdata;
     public static String[] getData(){
         return somedifferentdata;
     }
}

public class C{

   ....

    public void createPanel(Int arg1, int arg2..... , SOMETHING mydatasource){
              .....
              JComboBox combo = new JComboBox(mydatasource.getData());
              ....
    
    }
}

}

Ok I think I found something that works. Although it feels kind of dirty, lol. But now I can pass A.class or B.class or whatever I want that has a function with the given signature and I think it should work.

public class C{

   ....

    public void createPanel(Int arg1, int arg2..... , Class mydatasource){
              .....
      Method m = c.getDeclaredMethod("getData", null);
      Object o = m.invoke(null, null);
              JComboBox combo = new JComboBox((string[]) o);
              ....
    
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.