본문 바로가기

emotional developer/detect-Java

Using of Collections.emptyList() the right way in Java 1.5+

http://gochev.org/2010/07/using-of-collectionsemptylist-right-way.html

상당히 재미 있는 코드 스타일,
작은 것에서 차이가 날 수 있겠다.

Answer 5: The right but strange looking answer.

            Book myCrazyBook=new Book();

            myCrazyBook.setTitles(Collections.<String>emptyList());

Yes this is not a joke there is a dot (.) then the generic type before the method name. This compiles, runs, no warnings thats the real way. 
It is a little strange that most people dont know this aspect of generics in Java even I didn't know it, I was thinking that you cannot pass a generic type like this but as it looks you can.

So if you have some static method like this one:

public class Main{

      public static <G> List<G> someGreatMethod(){

            List<G> gList= new ArrayList<G>();

            return gList;

      }

}

you can invoke it with Main.<String>someGreatMethod(); for example.
반응형

'emotional developer > detect-Java' 카테고리의 다른 글

Build RESTful web services using Spring 3  (0) 2010.07.31
How to Conduct Effective Code Reviews  (0) 2010.07.28
Enum Types  (0) 2010.07.23