Java-Package Accessing Methods

Package Accessing Methods

There are two ways of importing the classes stored in a package. The first approach is to use the fully qualified class name of the class that we want to use. This is done by using the package name containing the class and then appending the class name to it using the dot operator. For example, if we want to refer to the class MyClass in the mypack package, then we may do so as follows :

Notice that MyClass is a class within the package mypack and is represented by separating the with dots. This approach is perhaps the best and easiest one if we need to access MyClass only or when we need not have to access any other classes of the package. But, in many situations, we may like to use many of the classes contained in a package. We may achieve this easily as follows :

This statement imports all classes of mypack package.

Program

502

Program Source

import mypack.*;

public class Javaapp {
    
    public static void main(String[] args) {
        
        MyClass  my = new MyClass();
        my.display();
        
        MyClass2  my2 = new MyClass2();
        my2.display();
    }
}

Leave a Comment