Unqualified name in Java
Asked Answered
G

5

11

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.

Germanophile answered 15/1, 2015 at 15:14 Comment(3)
This is an excellent Stack Overflow question: it's simple with a clear answer. Just because you can find the answer elsewhere (in my opinion), doesn't mean SO shouldn't have an answer. I have not looked to see if it's a duplicate.Safeconduct
@Safeconduct Too bad it shows zero research effort. That is a "strong" reason for some people to downvote.Swaraj
I'm with you; however, the other comment (which I flagged; against which the above comment was written), was plain mean, not constructive, and gave SO a bad rep.Safeconduct
M
20

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.

Mesothorax answered 15/1, 2015 at 15:18 Comment(0)
E
5

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

Estuarine answered 15/1, 2015 at 15:18 Comment(0)
A
2

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

Achilles answered 15/1, 2015 at 15:19 Comment(0)
T
1

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.

Tiertza answered 15/1, 2015 at 15:22 Comment(0)
S
0

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.

Safeconduct answered 15/1, 2015 at 16:21 Comment(1)
Unqualified names are also used when evaluating the cases of a switch() structure with predefined enum constants.Itchy

© 2022 - 2024 — McMap. All rights reserved.