File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ridiculous use of generics in K&B page 626

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Book doesn't provide any more information other than given below.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, What is the culprit in this code ? It looks & compiles fine
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"?" mark is not mandatory.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ridiculous" is used in K&B book. Wondering, which statement is bad
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:"ridiculous" is used in K&B book. Wondering, which statement is bad


None of them are.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic