Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer

The other reason (except collissions) would be if you need a class only once. This is especially true if the class implements a common interface and you only need the real classname for instantiation. Using the fully qualified name makes it easier to recognize that an uncommon class is used here, and where to find it (assuming sane naming conventions).

List li = new com.foobar.veryspecial.MagicList();
share|improve this answer
    
Intriguing answer. I like that. –  Brandon Mar 12 '13 at 18:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.