Jump to content
  • Advertisement
Sign in to follow this  
heh65532

Generic method in Java

This topic is 640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by heh65532

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?)

Share this post


Link to post
Share on other sites

What errors? Be specific.

 

errors are in the comments of the code

 

So I need to learn reflection and erasure.

 

thx

Share this post


Link to post
Share on other sites

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"

Share this post


Link to post
Share on other sites

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

Edited by Alberth

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!