≡ Menu

Java Package

Java Package
Welcome back folks, today’s Java tutorial will cover the Java package. What is a Java package? Why is it useful? What does it look like in code? These are the questions I’ll be answering today!

What is a Java Package

Well folks, Java packages are the means by which we organize our code. As a project grows and grows, we’ll inevitably have more and more Class files (.java files). So the way to think of a Java package, is like a folder in your regular Windows/Mac file system. It is used to “package” together all of your files into one logical place! It’s really just that simple boys and girls :)

Why are Java Packages Useful

Don’t get me wrong, it’s entirely possible to not use any packages at all and just shove all of your project’s files into one spot. But this is kind of like storing all your music files and pictures right onto your desktop without organizing them into folders… it’s just messy. You can get away with it if you have a tiny program, such as a simple Java tutorial program. Anything bigger than that (let’s say 5 Class files or more), then it might make sense to start creating packages.

What do Packages look like in Code

I’m glad you asked, let’s take a look at Spring STS and they way they display packages:

Java Package - 02

Alrighty, so here we see that Spring STS has a “Package Explorer” view. If you don’t see this in your Spring IDE, then try clicking on the following menus:

  • Window -> Show View -> Package Explorer (or Other, then type in Package Explorer)

So, what you see above is essentially the equivalent of having the following directory structure in your file system:

-> com
  -> howtoprogramwithjava
    -> business
      -- Bus.java
      -- Car.java
      -- Vehicle.java
    -> runnable
      -- MyProgram.java

 
If that makes sense to you, then your next question would probably be “Now that I understand WHAT a package is, HOW do I create them in MY project?“. Piece of cake, all I did to create these packages was right click on my “src” folder (or the root HowToProgram folder) and choose New -> Package. You just need to type in your packages names separated by periods (.) like so:

Java Package

By convention, package names should be entirely lowercase. They don’t have to be, but it’s just how everyone else does it, so you might as well be like the rest of the programming herd! Also, another convention is that if you’re developing a Java project for a website, you generally name your packages after the website address in reverse order. So my website is howtoprogramwithjava.com… so in turn, the packages I would create are “com.howtoprogramwithjava.someotherpackage”. Again, just a convention, but it makes it easier for any new programmer to jump into your code and understand everything with relative ease.

What do Java packages look like in code?

Alright, so now that you have a good understanding of what these packages are all about, let’s see how they translate in code. It’s pretty simple really. Any Class file you’ve created that exists inside of a package needs to have the declaration of that package at the very top of the file. So, in the case of my Vehicle Class file, the code will look like this:

package com.howtoprogramwithjava.business;
 
public class Vehicle
{
  // code removed for the sake of learning about packages only!
}

That’s all there is to it, since the Vehicle Class file exists inside of the “com.howtoprogramwithjava.business” package, it needs to have that declared at the very top. And yes, it has to be at the top, otherwise your code won’t compile properly. The first thing you’ll see in any Java file is the package declaration, but what’s tricky is that most Java tutorials online or in books leave this declaration out (simply because they assume that you know what a package is). So now I’ll be the one to tell you that that’s the case, I won’t assume you know this ;)

Have I beat this concept into your heads now? Will you always remember what a package is, and that you should use them to keep things neat and organized? Do they seem straight-forward enough to you? If not, then by all means ask me questions in the comments section. If you’re too shy to post a comment for everyone to see, then perhaps you’d prefer to email me directly at info@howtoprogramwithjava.com, that’s cool too! Or if you just want to check out more info on packages, the wiki page on this topic is pretty good too :)

{ 8 comments… add one }

  • tudza August 19, 2012, 7:33 am

    So what convention do you follow if the code is not for a web site?

    • Trevor Page August 19, 2012, 11:59 am

      If the Java code you’re making is not for a website, then you’ll need to consider creating some sort of UI (User Interface) component to your program. You can Google packages like AWT (Abstract Window Toolkit) or Swing for Java. This topic is in my queue to discuss in more detail, so keep an eye out for that!

  • Jesse August 19, 2012, 2:43 pm

    Thanks again for another great tutorial. Keep them coming!

  • 1eye May 11, 2013, 4:27 am

    Awesome tutoirials :) Is it possible to call a outside package into another program ? If so, would both packages need to be declared?

    • Trevor Page May 11, 2013, 10:22 am

      It is indeed possible to make use of an ‘outside package’. All you’d need to do is download the JAR file that belongs to the library (which contains the package you want to use) and add the library to your classpath. You may or may not have seen me do this already, but if you haven’t, then check out some of my assignment walkthrough videos on this blog and check out the instructional videos I have recorded. In these videos you’ll see me add the JUnit.jar file to my classpath and then import some of the packages for use.

  • Jane Hayes November 13, 2013, 10:32 am

    Trevor – I have been working my way through your tutorials, they are great!

    Thanks.

    • Trevor Page November 13, 2013, 2:16 pm

      Thanks Jane, pleasure to be of service to you :)

      • Trevor Page November 20, 2013, 11:42 am

        Awesome question Gil,

        The easiest way for me to explain this is that the equals() method that belongs to the Object class just compares the actual physical memory locations. This is done like so:

        public boolean equals(Object obj) {
          return (this == obj);
        }
        

        See how it just uses the “equals equals” operator? All that will do is compare memory locations of the objects.

        This means that if you create two User objects with the SAME username, and you do NOT override the equals functionality, it will perceive these objects as different (as they are indeed two different objects in memory).

        This is why I introduced the override on the User‘s equals method. If you look carefully at my code, you’ll see that I’m telling Java to compare the User objects not by their memory address using == but rather their username properties.

        Does that help to clear things up?

        As for your follow up question about overriding inherited methods… I’m not sure I understand what you mean by “doesn’t necessarily fit”

Leave a Comment

Powered by sweet Captcha

Next post:

Previous post: