Import statement in java is used in java to import all the predefined classes defined in a package and within that classes many methods are defined. Package is just like the directory in the file system.  Whenever we are writing any import statement means we are telling compiler to load all the classes or any specific class, it depends upon the import statement declaration.User can use any class or that specific class at any moment of time in their programs. Now i will tell you how to use import statement and how to import all the classes defined in different packages or any particular classes from a package.
Java languages follow the following sequence of definition.

Package ----- > Class -------> Methods

The above diagram indicates that packages contain many classes and then within those classes there are many predefined methods.

Now you must be wondering how java follow this type of structure?
It is just because to make it object oriented, we are calling these objects with its class name where it is been defined but if we define our own class and if it contain some methods then we need to create object to call all our methods. Again if method is defined as static then we can call it by its class name also, that is the reason why almost maximum predefined functions are declared as static so that we can use it just by its class name.
All import statement must be terminated with semi-colon ( ; ).

There are different ways of importing packages :-

1> Importing all the classes within a package.
<package_name>.* ;
for ex- java.lang.* ;
Here java.lang is a package definition and the asterisk character is used here to indicate the compiler to import all the predefined classes from that package.

2> Importing specific class from a package.
<package_name>.<class_name> ;
for ex- java.lang.Object ;
Here java.lang is a package name and Object is one of the class within that package. So here we are telling compiler that we want to use only Object class and we can use the methods defined in this class any where in our program.

Like java.lang there are many other packages.
for ex- java.swing, java.applet, java.awt, java.io, java.beans, java.math etc
and also
javax.sequrity, javax.swing, java.sql,javax.sound etc
any more packages are there .
If you want you can have a look on those packages and classes. Follow the following steps.

1> Go to that Drive where you have installed your JDK. In my PC i have installed in C:\ drive.
2>Now select the program files folder and double click on it.
3> now you find a java folder ,double click on it.
4>Now you will find two folder i.e JDK and JRE
5> Go to JDK folder where you will find a src folder.
6> In src folder you find various packages like java, com, javax etc and within that packages many classes defined.
7> Now have a smile in you face that you got the predefined classes that you are using in your programs.

In short follow this sequence diagram,

C: Drive ---> Program Files ----> Java Folder ---> JDK ----> src folder----> many packages like java, javax etc 

Now have fun with the import statement. Import any one and build your apps accordingly. 

2 comments:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic