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

Updated on: 03-Jul-2020

462 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements