2

My question is NOT regarding understanding the technical understanding of usage of Serializable interface. Those answers has already been explained [1] & [2] among others

My question is from syntactical point of view.

While watching the source code of Collection interface and its sub-interfaces I noticed most of the interfaces implements Cloneable and java.io.Serializable as shown below :

public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable{
...
}


public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
...
}

public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
...
}

...and so on

I also noticed :

  • Collection interface , TreeSet LinkedHashSet etc. belongs to package java.util;
  • Cloneable interface belongs to package java.lang;
  • Serializable belongs to package java.io;

Now my question is why are we adding the prefix java.io before Serializable(as shown above) and not import import java.io.Serializable; as done in Collection interface.

Community
  • 1
  • 1
KNU
  • 2,560
  • 5
  • 26
  • 39

4 Answers4

2

This is how Java imports work:

  • all classes and interfaces in java.lang are automatically visible.
  • any class or interface in the same package are automatically visible to other classes and interfaces in that package.
  • any class or interface in another package must be explicitly declared (package & classname) or imported.

Given these rules:

  • AbstractSet, NavigableSet are in the same package as TreeSet and therefore automatically visible.
  • Cloneable is in java.lang which is automatically visible.
  • Serializable is in java.ioand so explicitly declared (but would also work with an import).

In asking why you would use an explicit declaration or an import in most cases it's down to style. In most cases an import is used as it keeps the code cleaner.

The only exception to this would be when two or more classes or interfaces with the same name are being used. In this situation you should declare each use explicitly.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • as suggested by @Kayaman could it be really to distinguish from `sunw.io.Serializable` (in [this answer](http://stackoverflow.com/a/27963276/1851302)) – KNU Jan 15 '15 at 13:23
  • that's possibly why, though `sunw.io.Serializable` was deprecated many years ago and I'd certainly assume that when I see Serializable it's `java.io.Serializable`. – Nick Holt Jan 15 '15 at 14:04
2

The classes you have mentioned (TreeSet, LinkedHashSet etc.) doesn't have any imports at all and always use full class name if it belongs to the packages other than java.lang or java.util

I would say that it is a bad practice, but we cannot blame Mr. Josh Bloch :)

bedrin
  • 4,458
  • 32
  • 53
0

There is an interface sunw.io.Serializable, so they most likely wanted to distinguish those two. However it's impossible to say what was the actual reason for this. Maybe it was policy, maybe they had run into difficult to debug bugs due to importing the wrong one...

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

There is no great rationale behind this. The classes have no other import statements and Serializable is referred to at just one place in those files, so it is easy to see why the author may have preferred not to import.

As an another point of interest, TreeMap in OpenJDK 8 both contains the import statement and uses the fully-qualified name.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • My installed java 'jdk1.7.0_51' TreeMap doesn't have any import BUT uses `java.io.Serializable` as reported for other cases. – KNU Jan 15 '15 at 13:32
  • Given no special qualification you should assume that I am talking about the *current* OpenJDK version (it is 8 at the time of this writing). – Marko Topolnik Jan 15 '15 at 13:36