GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in Generic Method Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in Generic Method Declaration" Watch "Problem in Generic Method Declaration" New topic
Author

Problem in Generic Method Declaration

Bhoomika Bathla
Ranch Hand

Joined: Nov 28, 2009
Posts: 40
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

Joined: Oct 12, 2010
Posts: 42
Can try to make the class declaration as:



Devil is in the detail.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
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

Joined: Nov 28, 2009
Posts: 40
The following code will compile :-
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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

Joined: Nov 28, 2009
Posts: 40
I am confused.Please explain it further.I might have written something wrong ,since i am not good at Generics.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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

Joined: Jan 17, 2011
Posts: 179
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
 
Don't get me started about those stupid light bulbs.
 
subject: Problem in Generic Method Declaration