0

In some parts of my Program I need to pass around a set of Objects (also some primitive types) between my classes.

For every new combination I have to create a pretty "stupid" wrapper-class which just contains two or three fields. (int, String or today I've got a int, String, Date).

All they contain is an empty and a full constructor and each field has its getter/setter. I came across Java dynamic proxies which is praised as a solution for my problem. It appears to be impressive and mighty - I could find some fancy examples how to control robots or fetch changes in beans, yet nothing for my original purpose only to transfer a simple wrapper object between two classes.

Is there a tutorial for this kind of issue?

Qohelet
  • 1,459
  • 4
  • 24
  • 41
  • For one thing, you do realise that getters/setters/constructors are optional and can actually be harmful in cases like this? (And by "harmful" I don't mean your program won't work; I just mean it will be really annoying to write) – user253751 Feb 05 '15 at 09:49
  • 1
    have you consider yto use generics? – user902383 Feb 05 '15 at 09:51
  • A java proxy is for intercepting calls to an object that implements an interface.. which does not sound like what you want. – Chris K Feb 05 '15 at 09:52
  • building on user902383's suggestion, Scala does something similar. It creates N classes, all called Tuple1, Tuple2, Tuple3 and so forth and then uses generics on those to give the effect that you are after. – Chris K Feb 05 '15 at 09:53
  • @immibis Can you be more specific please? What do you mean by "harmful". Usually I just auto-generate most of the class in NetBeans – Qohelet Feb 05 '15 at 11:00
  • @ChrisK I have never worked with them before and so far no idea how to use them properly. But try googeling "Dynamic Wrapper Java" - the proxies are what you get... :-/ – Qohelet Feb 05 '15 at 11:02
  • @Qohelet an example use of a Java proxy would be say, if you wanted to log every time a method on an object is invoked (so long as that method is declared on an interface that is, as that is a restriction of java.lang.reflect.Proxy). What java.lang.reflect.Proxy does not do is dynamically create a new composite data type. In your situation I would consider user902383's solution, carry on as you are or use an Object array and drink a toast to type safety ;) – Chris K Feb 05 '15 at 11:09
  • The confusion with java.lang.reflect.Proxy probably comes about because java.lang.reflect.Proxy generates a wrapper object of a defined type (implementation detail). The goal of it however is to intercept method calls of a known type against an existing instance of an object. – Chris K Feb 05 '15 at 11:13
  • @Qohelet it's harmful in that it makes it harder to write your program, and has no benefits (in this case). – user253751 Feb 05 '15 at 21:25
  • @immibis: Why? Please be more specific – Qohelet Feb 06 '15 at 04:11
  • @Qohelet because you have to write all the getters and setters, and the constructor, and type slightly more each time you want to access a field. – user253751 Feb 06 '15 at 04:32
  • @immibis I barely write the getter/setter on my own. NetBeans does that for me. And usually it's really accurate =) – Qohelet Feb 06 '15 at 16:22

1 Answers1

2

As i meantion in comments, consider to use generic.

You need to have object which holds two custom types, here we go:

    class Pair<F,S> {
    private F first;
    private S second;

    Pair(F first, S second){
       this.first = first;
      this.second= second;
    }

   public F getFirst(){
   return first;
   }

   public S getSecond(){
   return second;
   }
}

How to use it? it is simple, Pair<String,Integer> pair1 = new Pair<>("This is One",1);

user902383
  • 8,420
  • 8
  • 43
  • 63
  • Actually, as you say it - no, I haven't thought of that yet. Great idea. Seems sometimes you can't see see the wood for the trees =) – Qohelet Feb 05 '15 at 11:09