0

I am using kundera to define my data model which will be stored in hbase. There is a class called "Task" which should have a generic type of submission like so:

public class Task {
    ...
    Object submission;
}

The submission could be of any type since I want to keep it generic.

So my question is: 1. Is the above method a good practice? Will it work? 2. What is the best way to achieve this?

Sun
  • 559
  • 2
  • 9
  • 30
  • Did you try it? Did it work? If it's the best approach? I don't know. Maybe you could try making the submission a generic type? public class Task { T submission; } – Erik Pragt May 08 '13 at 23:01

1 Answers1

1

Having a generic type is a good idea. Yes, it should work. However, the type you have provided is not generic. Here is an example of a generic type:

public class Task<T> {
    T submission;

    // You can now use T as a class (but not with `new` or some other things)
    public T getSubmission() { return submission; }
    public void setSubmission(T new) { submission = new; }
    public Task(T t) { setSubmission(t); }
    // etc.
}

Then, you can make a Task of a certain type, for example:

Task<String> stringTask = new Task<String>("hello");

Take a look at the Generics Tutorial for more information.

wchargin
  • 15,589
  • 12
  • 71
  • 110