Welcome!

From the Trenches of the Enterprise Software

Yakov Fain

Subscribe to Yakov Fain: eMailAlertsEmail Alerts
Get Yakov Fain via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Java Developer Magazine

Java Developer : Article

Java Basics: Lesson 11, Java Packages and Imports (Live Video Education)

Lesson 11 in the Hugely Popular "Java Basics" Series by JDJ Editorial Board Member Yakov Fain

I've been using the Eclipse IDE for creating the Ping-Pong example, but you do not have to use any IDE, and can create these programs using any plain text editor and a standard javac compiler. In this case you'll need to follow the following steps to create these programs in the packages engine and screen.

  1. Create a directory PingPong
  2. Create two subdirectories inside called engine and package
  3. Type and save the file PingPongTable.java in the directory screens
  4. Type and save the file PingPongEngine in the directory engine
  5. Get into the PingPong directory and compile both classes:
    C:\PingPong>javac screens/PingPongTable.java
    C:\PingPong>javac engine/PingPongEngine.java
  6. Now you can run the program as follows:
    C:\PingPong>java screens.PingPongTable
Please note that you have to use a dot as a separator (not a slash!) between the package and class names. Make sure that the system variable classpath includes the dot (see the very first lesson of this series) to let JVM know that it has to look for classes starting from the current directory.  

Naming Conventions

If you are starting a new project, you need to come up with the directory structures and the names for your packages and these are some considerations to keep in mind:

  • Name packages using small letters
  • You should try to make your package/class name combination unique. There is a simple rule to achieve this: if you work for a company Best Bananas that has a Web site bestbananas.com, the URL of this Web site is guaranteed to be unique. Use this URL in a reverse order as a prefix to all your packages: com.bestnananas. If the name of your project is Shipping, add this word or abbreviation after this prefix: com.bestbananas.ship. You can further separate classes based on their functioinality, for example:
    com.bestbananas.ship.invoicing
    com.bestbananas.ship.delivery.

    Directory structure will look as follows:

         com
            bestbananas
                  ship
                    invoicing
                    delivery

  • Think of the future uses of your packages. For example, if the invoicing package may be reused in a different application, do not include there classes that are specific to your Shipping application.
  • If you have some useful classes that perform such commonly used functions as date formatting or logging, create separate directory for them like util and log.

More Stories By Yakov Fain

Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

Comments (5)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.