Digital Edition

SYS-CON.TV
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.
About 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

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

I'm glad you mentioned the use of interfaces as repositories of constants. While it may not be recommended in the strict sense of why interfaces exist, I've used this technique for years on large projects as a good way of organizing and reusing constants.

So you can use wildcards in the import? Can you use static import for static classes?

Will the other 10 lessons be on SYS-CON.TV?

Is it Yakov Fain who is speaking at the moment? A live Java lesson - cool!

I used this lesson in conjunction with the last one, lesson ten on eclipse IDE and it really helped me




ADS BY GOOGLE
Subscribe to the World's Most Powerful Newsletters

ADS BY GOOGLE

Consumer-driven contracts are an essential part of a mature microservice testing portfolio enabling ...
To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitori...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, ...
Containers and Kubernetes allow for code portability across on-premise VMs, bare metal, or multiple ...
We are seeing a major migration of enterprises applications to the cloud. As cloud and business use ...
DXWorldEXPO LLC announced today that "IoT Now" was named media sponsor of CloudEXPO | DXWorldEXPO 20...
SYS-CON Events announced today that Silicon India has been named “Media Sponsor” of SYS-CON's 21st I...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, disc...
In this presentation, you will learn first hand what works and what doesn't while architecting and d...
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22n...
In his session at 20th Cloud Expo, Scott Davis, CTO of Embotics, discussed how automation can provid...
Everyone wants the rainbow - reduced IT costs, scalability, continuity, flexibility, manageability, ...
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT...
The standardization of container runtimes and images has sparked the creation of an almost overwhelm...
SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 1...
Most DevOps journeys involve several phases of maturity. Research shows that the inflection point wh...
Dynatrace is an application performance management software company with products for the informatio...
Today, we have more data to manage than ever. We also have better algorithms that help us access our...
Andi Mann, Chief Technology Advocate at Splunk, is an accomplished digital business executive with e...
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...