13

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?

Droo
  • 3,177
  • 4
  • 22
  • 26
  • 3
    What is the question? `` is not a generic method but the (compiler-generated magic) name of the special constructor method which does class initialization. – Dirk Apr 12 '10 at 21:32
  • Sorry...should have been more clear. I think I incorrectly assumed that the static block had something to do with the static generic type. – Droo Apr 12 '10 at 21:49

5 Answers5

22
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 functionally identical to an untyped empty list due to empty list being immutable and Java erasing generic types at compile time.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • @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. – Powerlord Apr 13 '10 at 14:28
  • 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 Apr 13 '10 at 14:38
  • @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? – Powerlord Apr 13 '10 at 15:00
  • Unicorns: Raw types are provided for backward compatibility. To use them is not idiomatic Java. – Tom Hawtin - tackline Apr 13 '10 at 16:31
9

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
  • 85,407
  • 12
  • 106
  • 123
  • I knew it was something that simple. The generic return type prefix is all that allows this? I learned something today... – Droo Apr 12 '10 at 22:18
  • 1
    If by "generic return type prefix", you mean the first "", 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 Apr 12 '10 at 22:33
2

<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 Maman
  • 30,277
  • 10
  • 88
  • 118
1

<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
  • 265,237
  • 58
  • 395
  • 493
0

For those who are looking for a similar solution, but for more current time (2023/Apr) where one has moved on to using List.of() as a terse replacement for Collections.emptyList()...

In the same way the type is prefixed to the method in

Collections.<ComplexObject>emptyList();

Use the same method-prefix pattern:

List.<ComplexObject>of();

chaotic3quilibrium
  • 5,661
  • 8
  • 53
  • 86