Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Packages - Defining

[an error occurred while processing this directive]

Package = directory. Multiple classes of larger programs are usually grouped together into a package. Packages correspond to directories in the file system, and may be nested just as directories are nested.

If you are writing a normal one-programmer application, it will typically be in one package. The purpose of a package is to group all related classes together. If you are working on a program that is divided into separate sections that are worked on by others, then each section might be in its own package. This prevents class name conflicts and reduces coupling by allowing package scope to be used effectively.

Library in package. If you're writing a library to use in various programming projects or by others, put it in its own package. Programs that use this library will be developed in their own packages. With its thousands of library classes, Java has grouped related library classes into packages, but usually you won't define more than one package.

Summary of how many packages you usually define

  • Normal programs = one package.
  • Library = one package.
  • Program + library = two packages.

Reasons for using packages

  • Limit the scope of class names.
  • [an error occurred while processing this directive] To make package access more usable.

Package declarations

Each file may have a package declaration which precedes all non-comment code. The package name must be the same as the enclosing directory.

Default package. If a package declaration is omitted, all classes in that directory are said to belong to the "default" package.

Here are two files in the packagetest directory.

package packagetest;
class ClassA {
    public static void main(String[] args) {
        ClassB.greet();
    }
}
and
package packagetest;
class ClassB {
    static void greet() {
        System.out.println("Hi");
    }
}

Note that these source files must be named ClassA.java and ClassB.java (case matters) and they must be in a directory named packagetest.

Compiling and running packages from a command line

To compile the above example, you must be outside the packagetest directory. To compile the classes:

   javac packagetest/ClassB.java
   javac packagetest/ClassA.java
To run the main program in ClassA.
   java packagetest.ClassA
or
   java packagetest/ClassA

In windows the "/" can be replaced by the "\" in the javac command, but not in the java command. Generally use a forward slash ("/") because it is used more commonly than the backslash in other places as well.

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.