How to Create and Access Packages in Java?

Creating Packages:

We must first declare the name of the package keyword followed by a program name. This must be the first statement in a Java source file(except for comments and  white spaces)Then we define a class

package firstPackage;
public class FirstClass
{
----------(body of class)
}

Here the package name is first package. The class FirstClass is now considered a part of this package. This listing would be saved as a file called FirstClass.java and located in a directory named firstPackage. When the source file is compiled, Java will create a .class file and store it in the same directory.

Creating  our own packages involve the following steps:

   ·         Declare the package at the beginning of a  file using the form

  package packagename;

   ·         Define the class that is to be put in the package and declare it public.

   ·         Create a sub directory under the directory where the main source files are stored.

   ·         Store the listing as the classname.java file in the sub directory created.

   ·         Compile the file.this creates  .class file in the sub directory created.

   ·         Compile the file.This  creates  .class file in the sub directory.

Java  also supports the concept of package hierarchy.this is done by specifying multiple names in a package statement ,separated by dots.

example: package firstpackage.secondpackage;

A java package file can have more than one class definitions.In such cases ,only one of the classes  may be declared public and that class name  with .java extension is the source file name.

Accessing  a  package:    

    

 The import  statement can be used to search a list of packages for a particular class.the general form of import statement for searching a class as follows:

import  package1 [.package2]  [.package3] .classname;

Here package1 is the name of the top level  package,package2  is the name of the package that is inside the package1 and so on we can have any number of package in a package hierarchy finally,the explicit class name in specified.

Note that the statement must end with a semicolum(:).the import  statement should appear before any class definitions in a source file.muliple import statements are allowed.

import  firstpackage.secondpackage.myclass;

After defining the statement all the members of the class Myclass can be directly accessed using the class name or its objects(as the case may be)directly without using the package name

We can also use  another approach as:

import  approach.*;

Here,package name may denote a single package or hierarchy of packages as mentioned earlier.the star(*) indicates that the compiler should search this entire package hierarchy when it encounters a class name.this implies that we can access all classes contained in the above package directly.

EX:

package package1;
public class ClassA
{
public void display A()
{
System.out.println(“class A”);
}
}

EX-2:

package package2;
public class ClassB
{
protected  int m=10;
public void display B()
{
System.out.println(“class B”);
 System.out.println(“m=”+m);
}
}

import package2.*;
import package1.class A;
class Packagetest
{
public static void main(String args[])
{
ClassA obj1=new class A();
ClassB obj2=new class B();
obj1.display();
obj2.display();
}

}

Related

Java 71083689601939521

Post a Comment

emo-but-icon

item