Skip to main content

package


                                                                Package
Packages:
1.      Introduction:
If no package name is specified in any java file then the class is part of the unnamed package. This requires that every class must have a unique name to avoid collision. After a while, without some way to manage the namespace, you could run out of convenient descriptive names for individual classes. You also need some way to be assured that the name you choose for a class will be reasonably unique and not collide with class names chose by other programmers.  Java provides a mechanism for partitioning the class name space into more manageable chunks. This mechanism is the package.
                      The package is both a naming and visibility control mechanism.
You can define class inside a package that are nor accessible by code outside that package. You can also define class members that are only exposed to other members of the same package. This allows your class to have intimate knowledge of each other, but not expose that knowledge to the rest of the world.

2.      Defining a package:
To create a package, include a package command as the firs statement in a java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If you omit the package statement, the class names are put into the default package, which has no name and is called un-named package.

This is the general form of package statement:
                                                                          Package pkg_ name;
Java uses file system directories to store packages. Remembers that case is significant, and directory name must match the package name exactly. More that one file can include the same package statement.

You can create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period. The general form of a multi-leveled package statement is shown here:

Package pkg_name1 [. Pkg_name3];
A package hierarchy must be reflected in the file system of your java development system.
For example a package declared as:
                       Package java. Awt. Image;
Needs to be stored in java/awt/image,  java\awt\image or java: awt :image  on your Unix, windows, or Macintosh file system, respectively.
A global naming scheme has been proposed to use the reverse internet domain names to uniquely identify package.
                                         Org.apache. tomcat
                              Which would be unique.
A package hierarchy represents an organization of java classes and interface. It does not  represent the source code organization of the class and interface. Each java source file (also called compilation unit) can contain zero or more definition of classes and interfaces, but the compiler provide a separate class file containing the java byte code for each of them. A class or interface can indicate that its java byte code be paced in a particular package, using a package declaration.

At most one package statement can appear in a source file, and it must be the first statement in the unit. Note that this scheme has two consequences. First, all class and interfaces in a source file will be placed in the same package. Secondly, several source files can be used to specify the contents of a package.

Comments

Popular posts from this blog

Menu bar example with applet viewer in java

      Menu bar +applet viewer example-3 import java.applet.*; import java.awt.*;  /*   <applet code="MenuDemo" height=200 width=200>   </applet> */ public class MenuDemo extends Applet {             public void init( )             {                         Menus m = new Menus("Menu Bar Demo");                                    m.resize(200,200);       m.show();             } } class Menus extends Frame {             Menus(String s)             {                         super(s);                         Me...

Java question

  Java ques interview Ques:-List any five features of Java?  Ans :-Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded Ques . write full form of jdbc & odbc ?  Ans :-  JDBC -java database connectivity.             ODBC - open database connectivity Ques:- what are types of packages ? Ans: 1. Pre-defined packages 2. User defined packages Ques :-Define class?  Ans:- A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object. Ques:- Denfie string bulider? Ans :- Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects. Ques :- What is Singleton class?  Ans:Singleton class control object creation, limiting the number to one but allowing the flexibility to create m...

MVC Example 1

                        Swing MVC Example in java Java Swing first programs In this chapter, we will program our first programs in Swing toolkit. The examples are going  to be very simple. We will cover some basic functionality. Our first example In our first example, we will show a basic window. import javax.swing.JFrame; public class Simple extends JFrame { public Simple() { setSize(300, 200); setTitle("Simple"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Simple simple = new Simple(); simple.setVisible(true); } } While this code is very small, the application window can do quite a lot. It can be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer. import javax.swing.JFrame; Here we import the JFrame widget. It is a toplevel container, which is used for plac...