Take the 2-minute tour ×

This has been bothering me a while, every time i look at some Java source code, i find myself surfing on folder that has folder that has folder that has folder in it etc. I dont understand why does Java require so many nested folders, which has nothing else in it except the new subfolder, and it just doesnt make sense to me.

For example: https://github.com/halfninja/android-dragcontrol3d/tree/master/src/uk/co/halfninja/android probably not worst example, but still there is two folders "uk" and "co" that just doesnt make sense. I see this kind of stuff 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;

so much cleaner.

I have never programmed on Java, so this might be stupid question... Excuse me.

share|improve this question
What if I made a gfx library, and you made one - there would be a conflict for whoever needed to use both. –  nos Jan 13 '12 at 15:12
What you describe intuitively as 'folder' has not this meaning. You can recognize they are domain names reversed: 'com.mojang' for the domain name 'mojang.com'. The purpose is exactly the same: providing a unique name. See: docs.oracle.com/javase/tutorial/java/package/namingpkgs.html –  mins Jun 21 at 21:05

8 Answers 8

up vote 7 down vote accepted

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?

share|improve this answer
wouldnt just one subfolder fix this problem? for example minicraft.gfx.Font ? would make most sense to me. –  Rookie Jan 13 '12 at 15:16
Yeah, but what you think is unique might not actually be so. In this case, yes, minicraft would probably be sufficient. The usual convention is three parts that represent the group responsible for the jar so that it would most likely not cause a conflict. It's about being safe rather than sorry. Since most IDEs can just auto import this stuff as well as go directly to classes, I really don't see why it's even a hassle. –  AHungerArtist Jan 13 '12 at 15:18
i was thinking, why not just use one folder afterall? com_mojang_ld22.gfx.Font will this cause any problems? at least there wont be so many subfolders. –  Rookie Jan 14 '12 at 11:47
Python has a different solution; you can create aliases for the packages you import. "import packageA as packageB" or "from foo import classA as classB". In case of name conflicts, rename one of them. I think I prefer this to Java's approach. –  procrastinate_later Nov 13 '13 at 18:22
See this question –  procrastinate_later Nov 13 '13 at 18:32

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.

share|improve this answer

It's merely an organizational technique for preventing namespace conflicts. Nothing more or less. Java package names match the underlying directory structure, so any organizational pattern at the package level will be reflected there. It's typical for teams to start their package names with their organization's name and wax specific. This is simply convention, but it's ingrained and should be followed absent a very good reason.

share|improve this answer

It's all about Namespaces. With 'Namespaces', you can create 2 classes with the same name, located in different packages/folders. This Namespace logic can also be used for creating 'Access Privileges', etc etc. Below are some links:

1) Namespace 2) Java Package 3) Java Package Naming Conventions

EDIT: Let us assume that you are creating a new project and are using 2 open source frameworks from companies/organizations - comA and comB. Also, let us assume that comA and comB have created a class in their projects with the same classname. Now, with the Java package naming conventions, we have com.comA.SomeClass and com.comB.SomeClass. You can import and use both the classes in your class, without having a conflict. This is just a simple example. There are other uses from this naming convention.

share|improve this answer

In Java, the convention is to name your packages (which correspond to the folder structure containing your code) with information identifying your organization (typically including a TLD and the company name) and project (which might add a few more sections).

Being more specific like this also reduces the likelihood of namespaces accidentally colliding with eachother.

share|improve this answer

If you want to share code with everyone else, but use generic names without conflict. its considered good practice to include you domain name (backwards)

Everyone write a package called gfx.Font you wouldn't be able to use more than one version in the same application.

You might feel your code will not be shared with the world (or even should not be shared) In which case, a shorted package structure may be simpler.

If you use an IDE, it does a good job of hiding long package structures so you don't need to worry about it.

share|improve this answer

Java does not require anything: you can just put all your classes in the default package and surf away. But for serious projects that kind of organization is not only wise, it's mandatory. The com.mojang.ld22 part is just a convention:

  • com = either this or org, java/javax for official packages
  • mojang = second part is company name
  • ld22 = third part is application name
share|improve this answer
i think the ld22 is the competition name, minicraft should be app name, which is missing for some reason –  Rookie Jan 13 '12 at 15:30
Of course it stands for Ludum Dare 22, he just chose to use that instead of the actual project name. There is no ambiguity anyway, and knowing Notch he probably decided the actual name halfway through the challenge. –  Viruzzo Jan 13 '12 at 15:34

This is due to recommended packaging structure. In large projects, so many packages/libraries are used and in order not to put source files into same folder with another library, programmers put their source codes into unique folders. As websites are unique, it is a convention to use packaging structure that looks like folder structure of websites.

share|improve this answer

Your Answer


