Question:
Import Packages and their Uses (in Java)?
little mons†er 1004
2012-02-15 04:23:58 UTC
what are the Import Packages in Java Programming and what are their used for.
Four answers:
Silent
2012-02-15 04:39:34 UTC
Packages are a way of organizing classes into related groups.



To use classes from another package, you must either use their fully qualified names (e.g., java.util.ArrayList) or import the class or package at the top of your source file. In most cases the second method is preferable; only use the fully qualified names in your code if you need to use two classes with the same name from different packages.
?
2012-02-15 04:34:49 UTC
Do you know C language?.

It is like include. In C, we use ready made functions by appropriately including the header files. While in Java, in order to use readily available classes developed by other, we import appropriate package.

We can say a package as a collection of classes, interfaces.
2016-05-16 13:52:19 UTC
To call a static method from a class without using an import statement you have to write the all path to that method using package name + class name + method name. In this case you would have: greatThings.GreatClass.doesItAll()
instanceof
2014-12-25 07:29:14 UTC
Package:



Packages are nothing but all the related classes and interfaces and .class files.

Packages are the folders which are binding all the related ".class" files together.

Binding: making something available to related functionalities.

A simple folder would only groups all the related files . But where as packages are also folders which binds all the related files.

Every package would be a folder. But every folder can not be a package.



How to create user defined package:



By using a keyword "package".

package package_name.



package instanceofjavaforus;

Class Demo{

public static void main (String args[]) {

}

}





The Directory Structure of Packages:



To make java compiler to crate a package for us and automatically load all corresponding files into package



package instanceofjavaforus;

public Class Demo{

public static void main (String args[]) {

}

}



Save this as Demo.java

At this time there is no folder with name instanceofjavaforus

Javac -d . Demo.java.

-d : creates directory

instanceofjavaforus -> Demo.java: a folder with name instanceofjavaforus will be created and inside Demo.class will be there.



Creating Sub packages:



Create a package with a class;



package pack;

public Class Demo{

public static void main (String args[]) {

}

}



C:/> javac -d . Demo.java

Now pack-> Demo.class folder structure will be created.

Now create sub package like this.





package pack.subpack;

public Class Abc{

public static void main (String args[]) {

}

}



c:/> javac -d . Abc.java

pack->subpack -> Demo.class,Abc.class



How to create jar file representing our package:



By using dos command

c:/> jar -cvf file_name package_name.

Ex: jar -cvf instanceofjava.jar pack



Importing a package:



We can import packages by using "import".

To import the package

import<.><* or name of class you want to use from that package

To import particular class inside a package>.

import<.>

As per the naming convention: For package names use small case letters.



import pack.Demo;

public Class Xyz extends Demo {

public static void main (String args[]) {

}

}





import pack.*;

public Class Xyz extends Demo {

public static void main (String args[]) {

}

}





Commonly using Predefined packages in java:

Java.lang:

Provides classes that are fundamental to the design of the Java programming language. like



Integer

boolean

Class

Object

Runtime

Thread

System

Process

Package

String



Java.util.*:

This package contains the collections framework classes, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous. some of the interfaces and classes present in java.util package are as follows.



Collection

Comparator

Enumeration

RandomAccess

List

Set

Map

Iteraot

EventListener

Scanner (class)



Java.io.* package:

This package provides classes and interfaces for system input and output through data streams, serialization and the file system.Some of the classes present in java.io package are as follows.



BufferedInputStream

BufferedOutputStream

BufferedReader

SerializablePermission

Console

File

FileReader

FilerWriter

InputStream

PrintStream


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...