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

Technological progress can be expressed as layers of abstraction - higher layers are built on top of...
When building large, cloud-based applications that operate at a high scale, it’s important to mainta...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, disc...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO Silicon Valley 2019 will cover all of these tools, with the m...
Big Switch's mission is to disrupt the status quo of networking with order of magnitude improvements...
Lori MacVittie is a subject matter expert on emerging technology responsible for outbound evangelism...
Dynatrace is an application performance management software company with products for the informatio...
In his session at 21st Cloud Expo, Michael Burley, a Senior Business Development Executive in IT Ser...
Having been in the web hosting industry since 2002, dhosting has gained a great deal of experience w...
NanoVMs is the only production ready unikernel infrastructure solution on the market today. Unikerne...
All in Mobile is a mobile app agency that helps enterprise companies and next generation startups bu...
SUSE is a German-based, multinational, open-source software company that develops and sells Linux pr...
Yottabyte is a software-defined data center (SDDC) company headquartered in Bloomfield Township, Oak...
Your job is mostly boring. Many of the IT operations tasks you perform on a day-to-day basis are rep...
Serveless Architectures brings the ability to independently scale, deploy and heal based on workload...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it wil...
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the c...
"Calligo is a cloud service provider with data privacy at the heart of what we do. We are a typical ...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (No...
Chris Matthieu is the President & CEO of Computes, inc. He brings 30 years of experience in developm...