Unqualified name in Java

5.5k views Asked by At

The teacher in our programming lessons is talking about "unqualified names", but I'm wondering what they really are.

I suspect that things such as method names are unqualified, but I'm not certain.

Is there anyone who can explain this to me? I need to know this because I need to explain in what way Java looks an unqualified name up.

5

There are 5 answers

0
Bohemian On BEST ANSWER

A qualified name is one with a full path, e.g.:

java.util.ArrayList list;

An unqualified name has just the last part:

import java.util.*;

ArrayList list;

The term can apply to fields and methods too.


So, if you can import classes, why would you ever need to use a qualified name?

You need it when you're using two classes that, while they're from different packages, share the same name. A classic example is the ridiculously named class from the JDK:

java.sql.Date

which incidentally extends

java.util.Date

It's reasonably common to need references to instances of both class, so you need code that looks like:

public void process(java.util.Date fromDate) {
    RowSet rows = <run query with fromDate as parameter>
    while (rows.nsxt()) {
        java.sql.Date date = rows.getDate(1);
        // code that needs date
    }
}

If you use two homonymous classes, there's no avoiding qualifying at least one - you can import one, but importing both creates an ambiguity.

0
Makoto On

A qualified name in Java includes the specific package that the class, interface, enum, or field originated from.

Example: java.util.ArrayList is a fully qualified name.

An unqualified name is simply the class, interface, enum or field without package information.

Example: ArrayList

0
josivan On

For example com.yourcompany.domain.Person is the fully qualified class name and Person is the class name or unqualified class name.

0
atish shimpi On

Qualified Name: org.springframework.jdbc.core.JdbcTemplate
It has package name org.springframework.jdbc.core then class name JdbcTemplate

Unqualified Name: JdbcTemplate It is only class name not having package name.

For example : qualified name is whole address of your home and unqualified name is only your home name.

1
Thomas On

Adding to the conversation a piece that has not been mentioned, yet (and that is not directly asked about but I believe is a helpful adendum to the conversation):

All names in Java require qualification; however, some are so integral to Java's operation that they are assumed - or, defaulted - to be "in the class" you are coding (or imported). Liskov (2000) gives a great example of this: java.lang - the class that contains such objects as String.

You often will see unqualified names in Java. This often has to do with the location of a class or method relative to the class in which you are attempting to access it (same package). Above, other posters have mentioned the concept of packages in Java.

Packages allow you to resolve - or, perhaps, better prevent - naming collisions. Each package in a program can duplicate the class names, etc. of another package. In this case, fully qualified names are used to access the correct class (as is seen in other answers). You can also import a package to avoid using its fully qualified name; however, should two imported classes contain a naming collision, you'll have a bit of problem.