In this tutorial we’re going to learn about packages usually in these tutorials. Now is the time I would say that I’m going to write click the default package and create a new class.
But in this tutorial we’re actually going to create a new package to do that. We right click the source folder and select new package by convention package names are lowercase.
What we want to do here is create a package to hold all the classes that we’ve made so far a good namemight be something like tutorials. And I’m going to click Finish and now we’ll see that an empty package called tutorials was created in the source folder.

Now for the most part you can think of a package like a folder like folders you can create another package inside of it to create a directory structure.
So it’s create another package inside of tutorials by right clicking tutorials selecting new package and we’re going to name this one tutorials.basic and click finish.

So now you can see that we have tutorials package and tutorials basic package.
What I usually do is go to this upper right arrow in the package explore go to package presentation and choose hierarchical.So lets move all of our classes inside of that package and going to select the top class.
Hold shift and select a bottom class to select all of them and just drag them into tutorials are basic.
And now you’ll see that the default package no longer exists and we’re left with tutorial that basic which we can expand. And now if we were to open up one of these classes will see that at the top. It now says package tutorials.basic.
So to give you an idea of what we just did and going to right-click the source folder and select new class and we’re going to name the class test and click finish.
And now notice that test was created in the default package and it has no package declaration like what was added to all of our other classes.
Now inside of test I’m going to create a main method by typing main and present control spacebar. And now let’s say we wanted to use our Person class that is inside the tutorials.basic package.
Well before we would type person person equals new person
and you’ll see that Eclipse is giving us an error and we hover over that error says that person cannot be resolved to a type.
In other words Java can’t find the person class and the first quick fix says inport person from tutorials.basic so we click that, we’ll see we’ve imported tutorials.basic.person
This is just like when we created a Jlabel in the past so we type Jlabel lbl and analyzed
that import and says import javax.swing.jlabel
What that means is there is a package called Java x inside of that package is a package called swing and inside of the swing package there is a class called jlabel.

So packages are great to help keep all your classes organized but that’s not their only purpose. They also allow you to avoid potential naming conflicts.
For example what would happen if we had two different person classes and they both did different things but we needed to use both of them in our program. How would we be able to differentiate them the way you would do it is to use what’s called the fully
qualified name.
Now what that is is actually taking the package. In this case it would be tutorials.basic and placing it before the type.

And we also place it on the constructor and now we do things this way we no longer need our import statement.
But now if we had another person class we could say Person 1 equals new person.
And from here we could use Eclipse to import our different person in class and then our two person objects would refer to different instances of different classes that happened to share the same name.

So for one last thing to do let’s manually create a package declaration in our test out of a file.
Now a package declaration has to be at the very top of the java file.
The only thing that can be above it is a comment.
So make sure you’re above our import statement and type package tutorials;
And now if we hover over tutorials eclipse will ask us if we want to move test out Java to package tutorials. Let’s click that

And now if we expand the tutorials package will see our basic package as well as our test java file. And from there we can expand our basic package and see all of our old classes.

Now I’m going to delete our tested java file by selecting it right clicking and delete and just click OK.
And now you can see our package structure went back to the way it was.

So as a final note in the last tutorial I introduced access modifiers and two of the access modifiers dealt with packages. They were protected in default.
So hopefully now you have an idea of how those might work. For example in our access modifiers class if we were to remove the private modifier for me in text it
would have default access and if we were to change public to protected content why Now both of these variables are visible to every class inside of our tutorials that basic package. If we created a class in the default package and created an instance of access modifiers these two variables would not be visible from that class because that class would be in a different package and the same would apply to any methods that happen to share those same access modifiers.
Thanks for watching.