Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Introduction of Errors
Java Exceptions
Java Try Block
Java Catch Block
Java Finally Block
Java Throw Keyword
Java Throws Keyword
Java Builtin Exceptions
User Defined Exceptions

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Packages


Packages in Java

What is Package?

Packages are used for grouping a variety of classes and interfaces together.

Packages are stored in hierarchical manner and are explicitly imported into new class definitions.

Example: Suppose, if we want to use the classes from other programs without actually copying them into program then this can be achived in java using packages. It is also a concept of reusability of code.

Benefits of using packages

  1. The classes contained in the packages of other programs can be reused.
  2. In package, classes can be unique. That means same name of package is not allowed but two packages can have same name.
  3. Packages provide a suitable way to hide classes. That means preventing other programs or packages from accessing classes, that means for internal use only.
  4. Packages seperats Design from coding.That means is is possible to change the all implementation of any method without affecting the rest of the design.

How to create and use package?

Steps

  1. Create an empty folder (package)
  2. Create an empty class inside a new folder and make it as public.
  3. Write package and package name inside the class before writting declaring of the class.Ex- package packageName;
  4. Create an actual program outside the new folder
  5. Write import statement with full packagename at beginning of the program.
  6. Done

1. Create an empty package (folder)

Goto your program directory and create an empty folder and name it.

Example - We have created MyPackage folder inside the MyJava folder in D:\

Java Package

2. Create a public class and write package package name in it.

Make a new class inside MyPackage folder and create a public class which is having package packageName as first statement.

For demonstration write an show() method as public.

Java Package

3. Write a actual program outside the MyPackage and import package

Write an actual program which is having a main() method, But Before this import the InnerClass with it's package name

Example : import MyPackage.InnerClass;

Java Package

4. Compile and run the main program

Just like normal java commands, compile the MainClass.java program using below steps

javac MainClass.java

java MainClass

Output will be displayed like this

Java Package

Compile and Run without using import statement

By using Fully qualified name

If you do not want to write import statement while using package then you can use that class using fully qualified name

Example : While creating an object we must specify the packagename.class name

MyPackage.InnerClass ic = new MyPackage.InnerClass();


<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo