| Author |
Ridiculous use of generics in K&B page 626
|
Saibabaa Pragada
Ranch Hand
Joined: Oct 24, 2010
Posts: 162
|
|
Hi, K& B Page 626, I see this sentence "This is a ridiculous use of generics and in fact you will see generics only rarely outside of collection". What does it mean ?
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6590
|
|
Given that I do not know the full context, I guess it means generics is being used generously in this class.
Outside of Collections, generics is indeed used sparingly.
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
Saibabaa Pragada
Ranch Hand
Joined: Oct 24, 2010
Posts: 162
|
|
|
Book doesn't provide any more information other than given below.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
Saibabaa Pragada wrote:Book doesn't provide any more information other than given below.
As Deepak said, Generics is mostly seen with the Collections. As far as I have seen, Generics is not used much elsewhere.
|
Mohamed Sanaulla | My Blog
|
 |
Saibabaa Pragada
Ranch Hand
Joined: Oct 24, 2010
Posts: 162
|
|
|
So, What is the culprit in this code ? It looks & compiles fine
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
Saibabaa Pragada wrote:So, What is the culprit in this code ? It looks & compiles fine
Is there any? I dont think so.
|
 |
Saibabaa Pragada
Ranch Hand
Joined: Oct 24, 2010
Posts: 162
|
|
|
"?" mark is not mandatory.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
Saibabaa Pragada wrote:So, What is the culprit in this code ? It looks & compiles fine
SCJP is full of examples that compile and run fine, but that would never be written that way in real life. There are lots of places you can use generics, but that doesn't mean it's worth the effort!
In this case, you're basically creating a class that can wrap any kind of object. But usually you could just use the object.
Collections are one place where they are really useful because you keep wanting to use the same sort of data stuctures (lists, maps etc) on different kinds of object.
|
 |
Saibabaa Pragada
Ranch Hand
Joined: Oct 24, 2010
Posts: 162
|
|
Hi Mat, I am still not clear. Here is the code that I can call to the TestGenericClass class. This looks good to me. If not, Could you please point out where I am going wrong ?
Matthew Brown wrote:
In this case, you're basically creating a class that can wrap any kind of object. But usually you could just use the object..
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
Saibabaa Pragada wrote:Hi Mat, I am still not clear. Here is the code that I can call to the TestGenericClass class. This looks good to me. If not, Could you please point out where I am going wrong ?
I don't think you're doing anything wrong. It's just an example to show how generics work. What K&B mean is that there would be little point doing this in a real project.
|
 |
Saibabaa Pragada
Ranch Hand
Joined: Oct 24, 2010
Posts: 162
|
|
|
K&B clearly mentioned "This is a ridiculous use of generics" means BAD..What makes it to be bad so that I can avoid using that part.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
Avoid it by using generics only when there's a good reason to. In the example they've given...why would you do that? What problem would you try and solve in that way? All they mean by it (I think) is that they've made up a very artificial example that is designed to illustrate how generics work rather than to solve a problem.
Don't worry so much about it!
|
 |
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
Code looks fine to me. I don't understand why it is bad as stated in the book. Maybe, somebody will let us know
Saibabaa Pragada wrote:K&B clearly mentioned "This is a ridiculous use of generics" means BAD..What makes it to be bad so that I can avoid using that part.
|
OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
It doesn't look especially ridiculous to me. However since it's a typical SCJP example with no relationship whatsoever to the real world, you wouldn't expect it to be especially meaningful anyway. And it isn't. It's rather pointless. But I wouldn't go so far as to call it "ridiculous".
|
 |
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
"ridiculous" is used in K&B book. Wondering, which statement is bad
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6590
|
|
Harikrishna Gorrepati wrote:"ridiculous" is used in K&B book. Wondering, which statement is bad
None of them are.
|
 |
 |
|
|
subject: Ridiculous use of generics in K&B page 626
|
|
|