File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor in Generic Class 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 "Constructor in Generic Class " Watch "Constructor in Generic Class " New topic
Author

Constructor in Generic Class

Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
The following is a code snippet for a generic class...



When we create an instance of this class, we will do as follows (for an example):



Why didn't we then write the constructor as:


I mean the <T> in public ABC<T>(T a, T b) {

Thanks

_Charles
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

You have already defined the generic at class level. It is not necessary to redefine it at method level. Does that answer your question ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well to make generic constructors or methods its not legal to define them like we do the class. Instead we do it with the boundry type like public <T> ABC(T a, T b). Why is it like this??? hmm think about it? If the Generic ABC class has a reference variable to itself then how it should be defined with the generic...... private ABC<T> a so doesn't it look similar to the Constructor. Well some restrictions are We cannot use it after the constructor and cannot be used to create an instance.

Well as Deepak said , its not necessary has it has already been defined in the class level. The boundary is used when it is not defined for the class i.e for methods.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
You have already defined the generic at class level. It is not necessary to redefine it at method level.


I got it. Thanks
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

Nitish Bangera wrote:
If the Generic ABC class has a reference variable to itself then how it should be defined with the generic...... private ABC<T> a so doesn't it look similar to the Constructor. Well some restrictions are We cannot use it after the constructor and cannot be used to create an instance.


could you explain this with a code please......

thanks in advance my friend.........


KARTHICK.C , SCJP6-93%
(Born to Win)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor in Generic Class