![]() |
![]() |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
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 |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
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.
Steps
Goto your program directory and create an empty folder and name it.
Example - We have created MyPackage folder inside the MyJava folder in D:\
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.
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;
Just like normal java commands, compile the MainClass.java program using below steps
javac MainClass.java
java MainClass
Output will be displayed like this
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();