Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic 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 "Generic" Watch "Generic" New topic


sundar sankar
Ranch Hand

Joined: May 30, 2007
Posts: 42
Hi All,
Where can find some example/notes for creating generic class and generi method.
Thanks in advance
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Java Tutorial ?
Olivier Croisier

Joined: Mar 14, 2006
Posts: 29
Or the excellent Sierra & Bates SCJP book...

SCJP 5.0 (100%)<br />SCWCD (getting ready...)
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
The recommendations by Manfred and Olivier are both good.

Here's the one I used (seems to be similar to Manfred, but is in PDF format)

If at first you don't succeed, try, try again. If you don't try you have failed.
Reet Goel

Joined: May 02, 2007
Posts: 14

I have a class called Basket with the signature
public class Basket<E>

Now if I say
Basket b1 = new Basket < Orange > ();

then what is the significance of parameterizing it with Orange type, as I can add any type to it and what it returns is of type Object.

It works exactly the same way,Basket b2=new Basket(); works.

am i right?

Please reply.

Riya Pant

Joined: Feb 17, 2006
Posts: 28
You should change your methods accordingly to take benefit from your generic class declaration.Where you are writing Object as return type or parameter type in the Basket's methods, you should write E.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Reet Goel:

Now if I say
Basket b1 = new Basket < Orange > ();
It works exactly the same way,Basket b2=new Basket(); works.

The idea is that the reference you assign to is also parameterized:
Basket<Orange> b3 = new Basket<Orange>();

Now you can only add Oranges in the Basket.

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Reet Goel

Joined: May 02, 2007
Posts: 14
Thanks Riya and Sergio.

I understand how Basket<Orange> basket=new Basket<Orange>(); works.

Actually, I specifically wanted to know if there's any difference between
Basket basket=new Basket<Orange>();
Basket basket=new Basket();

as I couldn't figure out any...

Please answer..

Thanks again
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15093

At runtime, there is no real difference between the two. Java generics work by type erasure: the compiler throws away the information about the parameterized type at compile time. Effectively generics are a compile-time only feature, just to check if you're not putting something in a collection that you shouldn't be putting in.

Doing something like this works without errors:

You will get a compiler error in the line where you are trying to put something in the basket:

Note: uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

You really shouldn't be doing something like "Basket basket = new Basket<Orange>();".

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Generic
It's not a secret anymore!