Skip to main content

Packages and Imports

Packages and Imports: Packages are directories with classes and methods, Import used to get the classes and methods from Inbuilt or created packages.

Syntax:                                       import package_name;

Here is the Code:

  1. //Package myPackage; (directory)
  2. // Java file in drive:Main.java
  3. import myPackage;

  4. class Main {
  5. public static void main (String[] args) {
  6. A a = new A();
  7. a.getMe();
  8. }
  9. }
  10. // Java file in drive:/myPackage/A.Java 
  11. package myPackage;
  12. public class A 
  13. {
  14.     public static void getMe()
  15.     {
  16.         System.out.println("Thank you");
  17.     }
  18. }
  19. /* compile:/myPackage> javac A.java 
  20. compile: javac Main.java 
  21. run: java Main 
  22. Output: Thank you */


Here is the Video: