views:

255

answers:

4

I return empty collections vs. null whenever possible. I switch between two methods for doing so using java.util.Collections:

return Collections.EMPTY_LIST;
return Collections.emptyList();

where emptyList() is supposed to be type-safe. But I recently discovered:

return Collections.<ComplexObject> emptyList();
return Collections.<ComplexObject> singletonList(new ComplexObject());

etc.

I see this method in Eclipse Package Explorer:

<clinit> () : void

but I don't see how this is done in the source code (1.5). How is this magic tomfoolerie happening!!

EDIT: How is the static Generic type accomplished?

+3  A: 
return Collections.<ComplexObject> emptyList();

Using this will get rid of warnings from Eclipse about non-generic collections.

Having said that, a typed empty list is going to be identical functionally to an untyped empty list due to empty list being immutable and Java erasing generic types at compile time.

R. Bemrose
@Tom Hawtin - tackline: "You may want to consider using this just to get rid of any warnings from Eclipse about using non-generic collections." In other words, this is correcting the code, rather than using a `@SuppressWarnings` annotation.
R. Bemrose
@OMG Unicorns Um, yes. Although the sentiment seems to be that warnings don't matter and do something random to get rid of them.
Tom Hawtin - tackline
@Tom Hawtin - tackline: I've reworded it now. However, it doesn't change that an **immutable** empty list that is typed is going to be identical to one that is untyped. In my opinion, the warnings about using non-generic collections are pointless anyway; if someone wants to use a non-generic collection, why should the compiler flag that as a warning?
R. Bemrose
@OMG Unicorns: Raw types are provided for backward compatibility. To use them is not idiomatic Java.
Tom Hawtin - tackline
+1  A: 

<clinit> is the name of the method into which the class initialization code is collected by during compilation. (That is, all of the code inside static {} blocks, and the initializers of static members, in source code order.)

It has nothing to do with explicit type parameters in method invocations.

erickson
+2  A: 

<clinit> is the static initializer block. It is a block of code which is executed exactly once (when the class is loaded).

So, instead of writing

class  A  {
   static int x = 5;
}

One can write:

class A {
   static int x;

   static {  // static initializer starts
      x = 5; 
   }
}

These two classes are equivalent. Inside a static initializer block one can place arbitrary code and thus initialize static fields with the results of complicated calculations.

Itay
+2  A: 

EDIT: How is the static Generic type accomplished?

http://www.docjar.com/html/api/java/util/Collections.java.html

public class Collections {
    ...
    public static final List EMPTY_LIST = new EmptyList<Object>();
    ...
    public static final <T> List<T> emptyList() {
        return (List<T>) EMPTY_LIST;
    }
    ...
}

You can see the link for the implementation of the EmptyList class if you're curious, but for your question, it doesn't matter.

Bert F
I knew it was something that simple. The generic return type prefix is all that allows this? I learned something today...
Droo
If by "generic return type prefix", you mean the first "<T>", then yes. I wouldn't call it that though - you shouldn't think of it as part of the return type. Think of it more as a (method) modifier like "public", "static", and "final". It's the parameterized type modifier on the method declaration that makes a generic method.
Bert F