Java Coding Standards – Reasons to Use Fully Qualified Class Names

coding-standardsjava

I recently ran across code where the developers used both fully qualified class names AND imported class names in their source code.

Example:

import packageA.Foo;

public class Example {
    public packageB.Bar doSomething() {
        final Foo foo = new Foo();
        ...
    }
}

I was under the impression that the only reason one might want to use fully qualified name for classes in source code is when they have identical class names in two different packages and they need the fully qualified name to distinguish between the two. Am I wrong?

Best Answer

No - you are quite right. Using fully qualified package names is usually considered poor style, except when it is necessary to avoid collisions.

If a package name is especially short and descriptive, using qualified identifiers can make code more expressive. But the JLS prescribes domain-based names for most packages, so package names usually aren't short and descriptive.