Generic method in Java

Started by
7 comments, last by ChaosEngine 7 years, 1 month ago

Hi

I'm learning to code in Java and having background of C++ I tried to create this generic method but I get lot of errors and don't know how to make this code work. can someone show me how it's done?

the code:


<T> Button createButton(String resourceName,String textName)
{
skin.add(resourceName, new Texture(textName));
T.TextButtonStyle buttonStyle = new T.TextButtonStyle();    // Error , cannot resolve
buttonStyle.up = skin.getDrawable(resourceName);
buttonStyle.down = skin.getDrawable(resourceName);
buttonStyle.checked = skin.getDrawable(resourceName);
buttonStyle.over = skin.getDrawable(resourceName);
buttonStyle.font = skin.getFont("default-font");

Button button = new T("", skin, resourceName );   // Error T cannot be instantiated directly
return button;
}

The code is supposed to make T be an different kind of Button object, for example TextButton. and then create button of that class and return it.

thx

Advertisement
What errors? Be specific.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
One of your problems is that in java you can not instatiate objects of the generic type. This is due to type erasure. Unfortunatley I dont have a lot of time right now but I am sure you will find a lot when googling about type erasure. Will check back later.

Edit: also note that while generics may look similar to templates they are very different.

One of your problems is that in java you can not instatiate objects of the generic type. This is due to type erasure. Unfortunatley I dont have a lot of time right now but I am sure you will find a lot when googling about type erasure. Will check back later.

Edit: also note that while generics may look similar to templates they are very different.

Just as he said, you cannot directly instantiate generic types. What you need to do is use reflection. Pass the type of the generic class as a parameter and then use reflection to call its constructor. Class<?> is the variable type for types. Beware that it is not safe to do that.

Hide yo cheese! Hide yo wife!

One of your problems is that in java you can not instatiate objects of the generic type. This is due to type erasure. Unfortunatley I dont have a lot of time right now but I am sure you will find a lot when googling about type erasure. Will check back later.
Edit: also note that while generics may look similar to templates they are very different.


Just as he said, you cannot directly instantiate generic types. What you need to do is use reflection. Pass the type of the generic class as a parameter and then use reflection to call its constructor. Class<?> is the variable type for types. Beware that it is not safe to do that.

Moreover, you can use factory pattern or suppliers, which can be kind of convenient in some places (I think that is what they were called in java?)

What errors? Be specific.

errors are in the comments of the code

So I need to learn reflection and erasure.

thx

I have a question about "?" usage in method. I was watching tutorial where that was used with a list like this:


void someMethod(List<? extends Number> numbers) {}

But when I tried to get that same working for single object (not list) it would not compile. can the "?" not be used with single objects then?

this:


void test(<? extends Number> num) {

}

gives error:

"wildcards may be used only as reference parameters"

Anything but the outer-most class gets erased in Generics, it's radically different from what C++ and C# do. Don't try to use it as C++ templates, it doesn't work that way, even if it looks like the same thing.

List<?> works, as the function signature is still complete (this means m(List<Integer> x) and m(List<Double> x) cannot be both defined, for example, as after type erasure, they are both m(List<?>) ).

The 'test' example has an incomplete signature, test(<?>) should be written as test(Object) if you want this. Each class gets compiled on its own, there is no "instantiate template on use" concept. There is 1 compiled version of the code that is used for all instances of the template (which is great for JIT of course).

Edit: It should be 'test(Number n)' of course :p

What errors? Be specific.


errors are in the comments of the code

Apologies. I didn't see them on the mobile site.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement