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. 

Related Posts:

  • Find the sum of all the numbers present in a String? This is a interview question can be asked in an experienced or fresher's interview to write a program which will add up all the integer value present in a string and print the added number. Tips to think how to do it...… Read More
  • Singleton class in Java This is one of the important and mostly asked question in fresher and experienced interview. Singleton class means a class with only one object. It will have only one object which will be returned always when ever we try… Read More
  • Show Programmatically that String is immutable ? This is one of the common question to be asked in interview to judge your basic level of knowledge. We all know that String is immutable which means that once object created cannot be modified or if try to modify, it will… Read More
  • Setting CLASSPATH in Java and configure it in windows ! Setting PATH and CLASSPATH are two different task in Java. Let me make you more clear about PATH setting and CLASSPATH setting in Java. If you had missed your lectures on PATH setting then this post will make you clear vi… Read More
  • Introduction to Java Like all other programming language it is also a well known programming language basically designed for web application . As we all know that c++ is an objected oriented language but not fully object oriented, an attempt mad… Read More

2 comments:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic