In this video, you will learn
about packages and imports. You will see how to
declare packages or create packages what
are charged packages how to use imports
and how we can import already existing
classes into our application and use
those classes. So let's get started first,
we are going to create a new project
in IntelliJ, so just go to file new project,
click next, next again and we need to
give a name to our project, I will call it
packages and imports, so packages
and import and just click finish. Now the new
IntelliJ window should appear, and let's
go to SRC directory. The directory
represents sources for our application.
so source code will be always in SRC directory.
Let's create a new package.
So right click new package and I will call it
academy.learnprogramming so academy., when you put
dot in package name, it will create two
packages, so it will create academy as
one package and child package to learn
programming, so academy.learnprogramming
and press ok. Now, in source
directory, we have two packages or we can
say we have parent package and child package.
Child package is learn
programming in this case, there is also
option in IntelliJ to compact empty
middle packages, since our academy
package doesn't contain any code we can
select that option and it'll compact
empty middle packages. As you can see it
basically removes our package and now we
can already see that Academy package is back.
Now, let's create a new Java class
and call it import example
the class will be in learn programming package
as you consider this package declaration
on top of the class.
Sometimes on exam they will not include
the packages or imports so you need to be
careful if they don't ask about imports,
they usually remove the imports to save
the space. So in that case you will see
some code that doesn't begin with line1.
As you can see in IntelliJ, on line1 we
always have package declaration. There is
another thing that you will often see
on exam questions and that is again to save
more space, they will merge code into one
line, so it might be confusing.
Don't allow that code to confuse you if the
code is merged into one line, you can
just rewrite it on paper, because on exam
you will not have IntelliJ or any other
IDE, you will just have paper and pencil.
So sometimes it's a good idea to rewrite
that code from the question on your
paper and format it nicely especially if
code is really hard to read or
understand. So we have seen child
packages, now if we again compact empty
middle packages and if you go to
packages for example, you will see
academy.learnprogramming there is dot
between and if we click hide empty
middle packages again, then we have empty
package academy sometimes when you have
a lot of classes it is good idea to
compact those empty middle packages.
I will just go back to the project view.
And let's create our main class, so PSVM
and pressing tab will generate our main
class and now we will use class random
to generate a random number and to bring
that number out. So random you will
notice that it shows you java.util
and that is the package where this class is declared.
Automatically when we press enter, IDE or
IntelliJ in this case, we'll import that
class as you can see there is import
java.util.random, so util
is charged package inside Java package and
random is class that is declared in that util package.
So random random is equal to new random
and now we want to print the random
number, so s out random.nextInt and
let's put the bound to 5 now if we run
that code, you will get the output some
number between 0 and 5. And as you can
See, we got the number 3 printed two cons.
If you run it again the number will be
different but the point here is we had
to import our random class. On the exam,
they might create packages with weird
names like a b c d for example, they
might just have for example some package
and then a.b,c.d, so don't be
surprised if you get some package names
like that. That is normal on the exam.
In real life you probably don't want to
create packages like that, every package
should have some meaningful name, so you
know what is inside that package.
What about system class? Why we didn't
import system class? And how Java knows
to use system class when it is not
imported? Well, system class is in Java
.lang package and the whole
.lang package is imported automatically.
And that package is special because it's
imported automatically, but our random
class it needs to be imported as you can
see if you hover on system, you will
notice there is java.lang and that
is the package where the system class is.
There are also other classes in java.lang package.
It's good idea to remember just few like
system or for example integers,
is in java.lang package and there
are many other classes. So let's see if
we try to import that class, import
java.lang and you can see a string,
string builder integer system and so on.
So if we import system, we will not get
any errors, but that import is just
redundant, we don't need to import that package.
We just need to import packages that are
not in java.lang system,
for example, is in package d, I create a new
class, let's say my class and now I want
to import the class into import example
application. I just need to type my
class and you will notice it shows a b c d
as a package. So don't be confused if
they even on exam as the import java.lang system,
you won't be confused by that.
I will just remove it for the simplicity.
Let's see what happens if we remove the
package or the import declaration, if I
comment out the input declaration,
we will get compilation error, cannot find
our resolve symbol random so java can't
find or the compiler cannot find our
random class, and we need to import it.
That is also star import. What is the
star import? Instead of important random,
for example we can import all classes in
util package, so java.util.
you can see there collection, arrays and
other classes, if we put star that will
import all the classes in that package.
So we can use all the class. Let's create
in package d another class, for example
let's say example class,
and now if we want to use both example
class and my class, let's add my class
even notice there is import abcd
myclass. If we put star and when we try to
type example class, you will see that it
automatically offers the example class
and we don't need to import anything.
So don't be confused by that star import, now
we can remove that import.
The other thing about imports is they need to be
at top of the class or above class,
you can add import first and then package,
that will give you compilation error.
We will see other errors a bit later in
this course, and how to structure the code.