Can we define a package after the import statement in Java?


No, we cannot define a package after the import statement in Java. The compiler will throw an error if we are trying to insert a package after the import statement. A package is a group of similar types of classes, interfaces, and sub-packages. To create a class inside a package, declare the package name in the first statement in our program.

Example

import java.lang.*;
package test;

public class PackageAfterImportTest {
   public static void main(String args[]) {
      System.out.println("Welcome to Tutorials Point !!!");
   }
}

Output

PackageAfterImportTest.java:3: error: class, interface, or enum expected
package test;
^
1 error
raja
Published on 07-Aug-2019 12:53:58
Advertisements