Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating generic method names in generic class?

Tags:

java

generics

Currently, I have something like this:-

public class MyHolder<T> {
    private T   value;

    public MyHolder(T t) {
        this.value = t;
    }

    public T getValue() {
        return first;
    }

    public void setValue(T t) {
        this.first = t;
    }
}

With this, I can use it like this:-

MyBean bean = new MyBean();
MyHolder<MyBean> obj = new MyHolder<MyBean>(bean);
obj.getValue(); // returns bean

Instead of calling the getter/setter to be getValue() and setValue(..), is it possible to "generify" that too?

Essentially, it would be nice to have it getMyBean() and setMyBean(..), depending on the type passed in. Granted this is a very simple example, however if I create a generic holder class that takes N generic properties, then it would be nice to call it something meaningful instead of getValue1() or getValue2(), and so on.

Thanks.

like image 923
limc Avatar asked Feb 10 '11 20:02

limc


1 Answers

No. There is no such feature in Java. I can't even imagine how it would look syntactically... void set<T>();? And how would the getter / setter for for instance MyHolder<? extends Number> look?

like image 195
aioobe Avatar answered Sep 22 '22 02:09

aioobe