Fully qualified V/S unqualified name in java

November 24, 2011 | Java

Java

Lets make it clear what is meant by fully qualified name & unqualified name in Java, and how to get them inside Java code

Consider a small example:

package org.bss.pkg;
 
Class Member {
 
    private int id;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
}

Here fully qualified name of ‘Member’ class is: org.bss.pkg.Member
One can get the fully qualified name of Member class, using the following snippet:

System.out.println("Fully-qualified name of Member class is: " + Member.class.getName());

Unqualified name of ‘Member’ class is: Member
One can get the unqualified name of Member class, using the following snippet:

String name = Member.class.getName(); // Get Fully-qualified name
 
// If the qualified name contains package structure, 
// get the class name by chopping off the package
if (name.lastIndexOf('.') > 0) {
    name = name.substring(name.lastIndexOf('.') + 1);
}
System.out.println("Unqualified name of Member class is: " + name);

Tags: , ,

Leave a Reply

If you have a Tip, Comment, Article or anything you want to share, please send it to: i@poisure.com