Explain package and import keyword of Java language

 package keyword 

            package  <pkg1>.<pkg2>.<pkg3>—–<pkgN>;

            package is  standard inbuilt keyword of Java language. It is used to assign package (folder path) to any java class. In the above syntax pkg1, pkg2, pkg3—– pkgN are package names.

            Whenever any class is assigned a package then it is necessary to keep that class file in the defined package.

 

Example:-

 package  java.awt;

            In the above example awt is sub package present in the java package. package keyword will assign java.awt package to the class file in which it is defined.

import keyword

             import <pkg1>. <pkg2>————<class name >;

                                    Or

            import <pkg1>.<pkg2>————-<pkgN>.*;

            import is standard inbuilt keyword of java language. It is used to import single class file or multiple class files from different package to current package.

            In the first syntax import keyword will import single class file and in the second syntax import keyword will import all the class files of given package.

Example

import java.awt. *;

 

In the above example import keyword will import all the class files of the java.awt package.

import java.awt.Button;

In the above example, import keyword will import only Button class from the java.awt package.

Leave a Reply

Your email address will not be published. Required fields are marked *