PACKAGES:
- A package is a collection of related classes.
- It helps Organize your classes into a folder structure and make it easy to locate and use them.
- More importantly,It helps improve re-usability.
Syntax:-
To create a package
Step 1) Copy the following code into an editor
package p1;
class c1{ public void m1(){ System.out.println("Method m1 of Class c1"); } public static void main(String args[]){ c1 obj = new c1(); obj.m1(); } }
Step 2) Save the file as Demo.java. Compile the file as, javac – d . Demo.java
Step 3) Run the code as java p1.c1
To create a sub-package
Step 1) Copy the following code into an editor
package p1.p2;
class c2{ public void m2(){ System.out.println("Method m2 of Class c2"); } public static void main(String args[]){ c2 obj = new c2(); obj.m2(); } }
Step 2) Save the file as Demo2.java. Compile the file as javac – d . Demo2.java
Step 3) Run the code as java p1.p2.c2
How to create a package
Suppose we have a file called HelloWorld.java, and we want to put this file in a package world. First thing we have to do is to specify the keyword package with the name of the package we want to use (world in our case) on top of our source file, before the code that defines the real classes in the package, as shown in our HelloWorld class below:
package world; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
Sub-package (package inside another package)
Assume we have another file called HelloMoon.java. We want to store it in a subpackage “moon”, which stays inside package world. The HelloMoon class should look something like this:
package world.moon; public class HelloMoon { private String holeName = "rabbit hole"; public getHoleName() { return hole; } public setHole(String holeName) { this.holeName = holeName; } }
How to use package
There are 2 ways in order to use the public classes stored in package.
1. Declare the fully-qualified class name. For example,
…
world.HelloWorld helloWorld = new world.HelloWorld();
world.moon.HelloMoon helloMoon = new world.moon.HelloMoon();
String holeName = helloMoon.getHoleName();
…
2) Use an “import” keyword:
import world.*; // we can call any public classes inside the world package
import world.moon.*; // we can call any public classes inside the world.moon package
import java.util.*; // import all public classes from java.util package
import java.util.Hashtable; // import only Hashtable class (not all classes in java.util package)
Thus, the code that we use to call the HelloWorld and HelloMoon class should be
…
HelloWorld helloWorld = new HelloWorld(); // don’t have to explicitly specify world.HelloWorld anymore
HelloMoon helloMoon = new HelloMoon(); // don’t have to explicitly specify world.moon.HelloMoon anymore
…
Note that we can call public classes stored in the package level we do the import only. We can’t use any classes that belong to the subpackage of the package we import. For example, if we import package world, we can use only the HelloWorld class, but not the HelloMoon class.
Example:
C:\aa c:\aa\bb
import bb.* \\or import bb extends B class A { public static void main(String args()) { B b1=new B(); System.out.println(b1.i); } } package bb; public class B { Public int i=10; }
0 Comments:
Post a Comment