9

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?

asked Jan 24, 2012 at 3:41

2 Answers 2

11

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.

answered Jan 24, 2012 at 4:06
1
  • I always use full qualified name because prevent collision (as you said) Commented Apr 29, 2015 at 14:56
11

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();
answered Jan 24, 2012 at 11:02
1
  • Intriguing answer. I like that. Commented Mar 12, 2013 at 18:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.