This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about the generic method

 
VuTuan Tran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.import java.util.*;
2.public class CreateAnArrayList {
3.public <T> void makeArrayList(T t) { // take an object of an unknown type and use a "T" to represent the type
6.List<T> list = new ArrayList<T>(); // now we can create the list using "T"
8.ist.add(t);
9.}
10.}

At line 3. what is the meaning of this public <T> void.I am confusing because unknown type T goes with void in this case.Usually, void is return nothing.
Please help me,thanks
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you are familiar with this Java™ Tutorials section.

Your method creates a List, adds a T to it, then the List disappears when it goes out of scope, and might be garbage-collected. So it doesn't do anything useful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic