devxlogo

Avoiding Confusion about an Import Statement

Every source file in Java (.java file) contains one class. You can bundle a set of related classes into a package. Java provides “import” keyword, which is used to establish a reference to a class in a different package.

There are couple of ways in which you can use “import”statement.

(a) Use a specific class name in “import” statement:

  import myPackage.myClass;	// myPackage-Package,myClass-Class

(b) OR you can use wild card “import” statement like :

  import myPackage.*; 	// import all the classes

The second approach is simple, because it imports all the classes in the specified package. There are no disadvantages of using wild card “import” statement with respect to compile time, run time, or code size, etc.

The only problem where we cannot use “import” is the place where we want to import two classes of same name from two different packages. In this situation, you can refer to the classes using full qualifier, i.e.

  myPackage.myClass.myMethod().

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.