The short answer is, Packages help keep the project structure well organized, allow you to reuse names (try having two classes named Account ) and are a common convention for large projects very much . They are nothing more than folder structures, but why they are used can burn newbies pretty badly. Oddly enough, with a project of less than 5 classes, you probably will not need this.
What exactly does this line do? Why is there, what is this role.
Line
package org.processing
tells Java that this class file lives in a folder named / org / processing. . This allows you to have a class that is fully defined as org.processing.Processor here, and in another folder - let's say / org / account / processing , you can have a class that is fully defined as org.account.processing.Processor . Yes, both use the same name, but they will not collide - they are in different packages. If you decide to use them in the same class, you will need to specify which one you want to use, either using either the import statements or the full name of the object.
Does this mean that compilation always refers to the directory structure?
Yes. Java and most other languages ββhave a concept known as a class path. Any of this class of the path can be compiled and launched, and by default the current directory in which you are located is in the class path for compilation and execution. To put other files in the classpath, you will have to use a different command line call for your compilation:
javac -sourcepath /path/to/source MainClass.java
... and this will compile everything in your source path into your current directory, neatly organized in the folder structure specified by your package operators.
To start them, as you already installed, you will need to include the compiled source in your classpath, and then execute through the fully qualified name of the object:
java -cp /path/to/source org.main.MainClass
Why is all this so?
As I said, this is mainly useful for very large projects or projects that include many other classes and a demand / organization structure, such as Android. He does a few things:
- Keeps the source in a simple structure. You have no objects scattered everywhere.
- Keeps the scope of your objects. If I had a package called
org.music.db , itβs pretty clear that I was messing around with objects that deal with database and persistence. If I had a package called org.music.gui , then he realized that this package deals with the presentation side. This can help when you want to create a new function or update / reorganize an existing one; you can remember what he does, but you cannot name him exactly. - It allows you to have objects with the same name. There is more than one type of
Map , and if you use projects that draw this in, you would like to specify which Map you will receive - again, by either importing or the full name of the object.