Search code examples
javaqualified-nameunqualified-name

Unqualified name in Java


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.


Solution

  • 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.