I want to make a generic container class that can contain one object of some other class. I thought this might be a reasonable approach:
class Container <T> {
private T thing;
public void Store(T obj) {
thing = obj;
}
public T ReturnIt() {
return thing;
}
}
When I try this together with let's say a Book class, I get the following error message: "Note: GenericContainer.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details."
Could it be that the public T ReturnIt() { return thing; }
is the cause of the error, and is this the wrong way to go about returning object that is contained in the Container-class?
I did not get any further information when I tried to compile it with -Xlint:unchecked.
What do I make of the error message?
Code that caused the error:
class GenericContainer {
public static void main(String[] args) {
Container BookStorage = new Container <Book>();
Book thejavabook = new Book("The Java book");
BookStorage.Store(thejavabook);
}
}
class Book {
private String title;
Book(String title) {
this.title = title;
}
}
class Container <T> {
private T thing;
public void Store(T obj) {
thing = obj;
}
public T ReturnIt() {
return thing;
}
}