Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Generic Method Declaration

 
Bhoomika Bathla
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was going through the Generics stuff and i come across the following question.The following code gives compilation error.It says :-
1. The method doubleValue() is undefined for the type X
2.The method replaceAll(String, String) is undefined for the type Y
3.The method byteValue() is undefined for the type Z

Can't we say X as Integer,Y as String,Z as Double.If not why ???What should be done to compile the code?
 
Jim Jonides
Ranch Hand
Posts: 42
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can try to make the class declaration as:

 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I would do, bear in mind I'm definitely not an expert on generics (working on it though ;)...



This way you still have your class Test able to take any types as arguments, it will only be the method check that is restricted to Integer, String and Double.

An alternative way, as you can see, is the commented out code. That is to say, you could remove the type specification from the method declaration, but add an instanceof check inside the method and perform your operations inside an if-block.
That way your method can handle any type of classes, with specific behaviour depending no the types, using instanceof checks.

// Andreas
 
Bhoomika Bathla
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code will compile :-
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Svenkson wrote:This way you still have your class Test able to take any types as arguments, it will only be the method check that is restricted to Integer, String and Double.

It wouldn't work that way. You're mixing generic classes and generic methods - the X, Y, Z in the method declaration are actually completely separate from the X, Y, Z in the method declaration. It might compile, but it's not doing what you expect, and will only confuse people. What you've written is actually equivalent to:

 
Bhoomika Bathla
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused.Please explain it further.I might have written something wrong ,since i am not good at Generics.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhoomika Bathla wrote:I am confused.Please explain it further.I might have written something wrong ,since i am not good at Generics.

The problem is that in your check() method the compiler has no idea what X, Y and Z are. You know you're going to pass an Integer, String and Double into it, but the compiler doesn't have a clue. So the compiler can't let you call methods like y.replaceAll(). If it did, what happens when you create another Test object, this time with a different Y?

There is a way round it, and that's what your second post does. There's nothing wrong with it***. It works. My reply was to Andreas' post.

*** Well, there is one thing. It's not "wrong" as such, and it's fine if you're just playing around with generics to see how they work. But since String, Integer and Double are all final classes, using generics is pointless here. Integer is the only class that satisfies X extends Integer, for instance. So if you removed the generics, and just declared the arguments as Integer, String, Double, the effect would be identical.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Andreas Svenkson wrote:This way you still have your class Test able to take any types as arguments, it will only be the method check that is restricted to Integer, String and Double.

It wouldn't work that way. You're mixing generic classes and generic methods - the X, Y, Z in the method declaration are actually completely separate from the X, Y, Z in the method declaration. It might compile, but it's not doing what you expect, and will only confuse people. What you've written is actually equivalent to:



Yes I know they are separate. I should probably have used other variable names though, and maybe it's not a good idea to do it like this, but I tried it and sure it works?

// Andreas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic