// Suppose we have a class called MyClass class MyClass { // Some code here } // We can get the name of this class using getClass() method and calling getName() method on it String className = MyClass.class.getName(); System.out.println(className); // Output: MyClass
// Suppose we have another class called MyOtherClass in a package called utils package utils; class MyOtherClass { // Some code here } // We can get the fully qualified name of this class using getClass() method and calling getName() method on it String className = MyOtherClass.class.getName(); System.out.println(className); // Output: utils.MyOtherClassIn this example, we're creating a class named MyOtherClass inside a package called utils and then getting its fully qualified name using getName() method. We can see that getName() method returns the package name and class name separated by a dot. Using getName() method, we can determine the package library of a class. In example 2, we can determine that MyOtherClass belongs to the utils package.