Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Java sources have so many folders inside each other?

Tags:

java

Every time I look at some Java source code, I find myself surfing in a folder that has folder that has folder that has folder in it etc. Why does Java require so many nested folders, which have nothing else in them except the new subfolder?

For example: https://github.com/halfninja/android-dragcontrol3d/tree/master/src/uk/co/halfninja/android That's probably not the worst example, but there are two folders "uk" and "co" that just don't make sense. I see this in Java sources only!

And for example minicraft: http://www.ludumdare.com/compo/ludum-dare-22/?action=preview&uid=398

import com.mojang.ld22.gfx.Font; import com.mojang.ld22.gfx.Screen; import com.mojang.ld22.gfx.SpriteSheet; 

Why not just write:

import gfx.Font; import gfx.Screen; import gfx.SpriteSheet; 

That's so much cleaner.

(I have never programmed in Java.)

like image 690
Rookie Avatar asked Jan 13 '12 15:01

Rookie


People also ask

What is the purpose of nested folders?

Nested subfolders allow you to organize and secure layers for yourself or your organization (if you have Admin permissions).

What are nested folders?

A folder stored within another folder. Technically, the nested folder is a "subfolder," and subfolders can also contain subfolders and so on up to a maximum level. After three or four levels, it becomes cumbersome for users to deal with. See folder, files vs. folders and Win Folder organization.

Which folder contains Java source code files?

Describes the Java™ source tree. When you create an API project, it is set up as a Java project with separate folders for source and class files. The source folder is named src . It contains the Java code of the application.

Are Java packages just folders?

A Java package is like a directory in a file system. In fact, on the disk a package is a directory. All Java source and class files of classes belonging to the same package are located in the same directory. Java packages can contain subpackages.


2 Answers

These are there to prevent conflicts with other jars. Having something like the company url in the package name makes it likely to be unique enough to not conflict with someone else's package and classes.

Your example is a good one, since it seems pretty reasonable to imagine two people thinking of using "gfx" as a package name and with classes like Font or Sprite. Now, if you wanted to use both of them, how could you since the package and class name would be the name?

like image 50
AHungerArtist Avatar answered Sep 22 '22 06:09

AHungerArtist


Your way is cleaner, but it assumes nobody else in the world is ever going to create a package called gfx, which is a pretty weak assumption. By prepending your reversed domain name, you create a unique namespace that avoids collisions.

This fits perfectly with the "culture of sharing" that pervades Java programming, in which applications typically combine large libraries from many sources.

like image 45
Ernest Friedman-Hill Avatar answered Sep 19 '22 06:09

Ernest Friedman-Hill