aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Declarations" Watch "Generic Declarations" New topic
Author

Generic Declarations

Faraah Dabhoiwala
Greenhorn

Joined: May 30, 2007
Posts: 21
Please explain me how to make our own Generic Class and Generic Methods?

its quite confusing


<b>Please don't visit my homepage...</b><br /> <a href="http://shadabworld.110mb.com" target="_blank" rel="nofollow">http://shadabworld.110mb.com</a>
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Let us know what you find confusing and we can narrow down on the problem.

Have you used some of the generic collections ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Faraah Dabhoiwala
Greenhorn

Joined: May 30, 2007
Posts: 21
1) Regarding generic class
Ok,,

So for example,,


here is a class



now here i understood that <T> is type it can be anything say,Integer,Long or say , Animal (provided Animal is a class)

so we can write




but how to use this class ..
why at all we define generic type after class name???

also there is another example where there are two generic types declared after class name




1) Regarding generic method




how to use this method?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Shadab Wadiwala wrote:1) Regarding generic class
Ok,,

So for example,,

here is a class



now here i understood that <T> is type it can be anything say,Integer,Long or say , Animal (provided Animal is a class)

so we can write




but how to use this class ..


The second case is not doing what you think it does. The first case declares a type T, which can be any type. The second case declares a type Integer, which can be any type. "Integer" is a type variable, and not refering to the Integer class.

why at all we define generic type after class name???


In these two examples, there is no reason to -- as you don't use the types anywhere.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

BTW, the Sun tutorial on Generics may be a good place to start ...

http://java.sun.com/docs/books/tutorial/java/generics/index.html

Henry
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

When you declare your class as rentalGeneric<Integer>, this doesn't mean that your class rentalGeneric has type java.lang.Integer. Try this



Basically you are just using Integer instead of T but that doesn't make your Integer the java.lang.Integer class. So don't use anything like Integer or String with your class declaration as it doesn't mean what you think it means.

As far as methods go, your code doesn't use generic methods very well. You've shown list.add as a statement in your method but where is this list declared. If a generic method is used in conjunction with generic class, then it works like this




Now if you want to use this class, then here's how you can use it



So here you declare your class to have Integer as the type of T, so you pass the display method a List of Integers. If you want to use a generic method which declares its own type, then here's how you can do it.



This method will accept an array list of a specific type and return a LinkedList of the same type. This is how you can call it



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Declarations