I was watching a lecture on Java when I caught myself not specifying package in Java file.
They lecturer was using Java 11 on Netbeans, and I Java 1.8 on IDEA. By default Netbeans created him a package, and the main java file has package specified.
But on IDEA, I noticed that I don't need to specify package name, when creating .java
file inside src\main\java
folder. And I have a question why?
- Why is src\main\java a folder, and not a package?
- What is the difference between a folder and a package?
- In Python, they create
__init__.py
file inside of a package, in order to make Python interpreter see the package. How is it working in Java, if there is no__init__
file? How does Java understand that this is a folder, and that is a package? - Why does Java need to introduce and separate folder and package terms?
CodePudding user response:
The difference between a package and a folder is not directly evident unless you define your classpath yourself.
All directories and all zip files you add to the classpath create a baseline. Such as the .../src/main/java
folder. Underneath that baseline you can still create folders with classes. But for Java these will be treated as packages.
So in the filesystem packages are resembled by folders. In zip files (and jar files are nothing but zip files with some metadata) there are no filesystem folders but still something folder-like that can be extracted into filesystem folders. And it is thinkable that people would write even other Classloaders that do not just do simple filesystem or zipfile access. They can do virtually anything which could even range to loading classes from different websites - just based on their package names.
As soon as a class is not in the default package (in a folder on the classpath directly) it needs the package name declared in it's source code. And that is what you spotted. Well done!
Summary: .../src/main/java
is the default package (that does not need to be specified) because that folder is added to the classpath when the JVM executes. Any classes in subfolders need their package defined.
A small detail: The classpath is set for the compiled classes, not the java source files. You could keep all java sources in the same directory and just declare by package into which 'folder' the compiled output should go. But it is good practice to organize the source files in the same structure that will be emitted by the compiler. Any IDE will help you maintain that structure.
CodePudding user response:
Why is src\main\java a folder, and not a package?
Which folders become packages depends on where the "root" is defined. In IDEA, src/java/main
is the root. Any folders inside of that are mapped to packages. If for example, you create a folder src/java/main/foo
and create a Foo.java
in that folder, you will have a class named Foo
in a package named foo
.
What is the difference between a folder and a package?
A package is a way to group related Java classes. Sometimes a package is implemented as a folder in your file system. Sometimes it is something else. (See below.)
Why does Java need to introduce and separate folder and package terms?
One reason to differentiate between folders and packages is that some packages aren't folders. For example, you can download a .jar
file from the Internet which contains a Java library. The library inside that .jar
file defines packages and classes that you can use in your own code, but there are no folders for the packages.
The reason for a new term "package" is to create an abstraction that isn't tied to the folders in your local file system.