Generics: Make sure parameters are of same type?

You can do that if you consider that S is the correct type.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

I've got the following method: protected void setValue(final S oldValue, final S newValue) { // Do something } I want to make sure, that both parameters are of the same type. It would be cool, if there'd be a compiler error when you try to pass parameters of two different types. The above way is clearly not the correct one.

I can put into a String and an Integer, since the both extend from Object. Is my want even possible? Or is the only way to make sure both parameters are of the same type to check it inside the method and throw an IllegalArgumentException?

Java generics methods parameters link|improve this question edited Feb 16 at 8:39 asked Feb 15 at 18:34Bernhard V1,089524 85% accept rate.

You can do that if you consider that S is the correct type : protected void setValue(final S oldValue, final T newValue) { // Do something } You can and can't input these : // Works setValue("something", "something"); setValue(new Object(), new String()); // Doesn't work setValue(new String(), new Object()); or You can do : protected void setValue(final S oldValue, final S newValue, final Class clazz) { // Do something } and use it like that setValue("something", "something", String. Class); or protected void setValue(final S oldValue, final S newValue) { if(!oldValue.getClass(). Equals(newValue.getClass())) { //throw something } }.

The side effect of the first is that you make the client spend extra effort determining the class of the objects they are dealing with. This won't be a problem if you always know that where you call this code, but if you ever wind up trying to use it where the code doesn't know for sure what the classes are, then you wind up with the same if statement as the last one, but before the method rather than in it... I think it's clearer to just do the input validation in the method and javadoc that it throws an exception if the types are not the same. – Gus Feb 15 at 18:46 You are right.

I added another more flexible alternative as the first. Although you can input something like setValue(new Object(), new String()) – Simon LG Feb 15 at 19:01 I like the second approach. – Bernhard V Feb 15 at 19:13 This is probably the best approximation you can get.

I like it. – Louis Wasserman Feb 15 at 20:25.

This isn't really possible, I'm afraid, except for explicit checks. It'll always get coerced up to Object; there's no way to stop inputs from getting coerced up to supertypes. You can use explicit reflection-based checking in your argument validation, but that's just about your only option.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions