11.2 Package

Cliquez içi pour plus de vidéos disponibles sur notre chaine youtube !

So, in the previous video, we discussed some coding conventions including classes, packages… And in this video, we’ll see what the packages are. The most important reason, which justifies the concept of package, is the management of name conflicts. Indeed, a Java project often uses several separate code libraries If both libraries offer a class with the same name, how do you control which one you want to use? Some reasons to use packages: This allows programmers to more easily determine that classes are actually linked Another good reason is that it is easy to know where to find classes and interfaces that can provide the functions provided by the package.. Managing name conflicts It helps to avoid. Conflicts between names So, we’ll create a package, it’s almost the same as creating a class except, we’ll choose package I’ll just call it p1. Now we have our package. so I can create a new file here. Then I’ll create a new Java class We’ll just call it maclasse. Here as we can see java by default put this package p1 up there It imported. So, if I wanted to use maClasse inside the main class. I’m actually going to have to import my package. If I want to use it as if I want to create an instance of maClasse. Then I can’t just do my class. maclasse is equal to new my class I get an error here and that says that my class cannot be resolved in type, in order to use this class as it is in a package. I have to tell java where this class is because Java doesn’t know where it is. So, what we can do is come here and import our package so I can just write import p1 point my class This tells java where my classes are and that I want to use Maclasse which is inside my package. But we can also. Click on our instance And make an import. So, it will directly import our package that was all for this video, I hope you understood by can what the package is and why it should be used. Come on! I’ll see you around.