2

I'm pretty new to Java, and I know what packages do. You use them to sort multiple files of a Java application together. However, is it standard to put it in a package if your application only has a single class? What are the pros and cons of doing this?

EDIT: I'm packaging this single class into a .jar file after.

ericw31415
  • 415
  • 6
  • 17
  • 1
    @bradimus That is a general example. I want to know for this exact situation, when you have a single class only. – ericw31415 Sep 01 '16 at 14:25
  • 1
    Unless the class is a throw-away, the default package is bad practice. Even then, how hard is it to type `package sandbox;`? – bradimus Sep 01 '16 at 14:35
  • 1
    If it's a simple test class that you're writing then default package is fine, unless you're using it in conjunction with other code in your code base that may cause conflicts. However, any other scenario should use standard package specification recommendations e.g. com.company... etc, or org.organization..., etc, especially if you're putting your code out there for others to use in the global space. – ManoDestra Sep 01 '16 at 14:39
  • 1
    @ManoDestra I guess that does make sense. I think I'll package it then. – ericw31415 Sep 01 '16 at 14:40
  • @bradimus It's kind of like: if I have a single file somewhere on my filesystem, I'll leave it there. I usually wouldn't create a new folder just to contain a single file. Now if I have single files elsewhere and I do the same, it would create clutter. – ericw31415 Sep 01 '16 at 14:43

4 Answers4

3

From oracle documentation, it is clear that

The primary motivation for jar development was so that Java applets and their requisite components (.class files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly improves the speed with which an applet can be loaded onto a web page and begin functioning. The JAR format also supports compression, which reduces the size of the file and improves download time still further. Additionally, individual entries in a JAR file may be digitally signed by the applet author to authenticate their origin.

From Package Documentation of Oracle,

For small programs and casual development, a package can be unnamed (§7.4.2) or have a simple name, but if code is to be widely distributed, unique package names should be chosen using qualified names. This can prevent the conflicts that would otherwise occur if two development groups happened to pick the same package name and these packages were later to be used in a single program.

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
2

It really depends on how you're compiling and running the program, but ultimately it's your choice.

Let's have a look at some of the different ways you might build your program.

1. Compiling the file with javac

If you're compiling the file using javac then the package will not matter. It will generate the .class file the same directory as the source file.

2. Compiling to a JAR File

If you're compiling to a JAR File, then the .class file will be inside the directories specified in your package name. Although this would not affect how the program is ran.

In both of these cases, I'd say that the package identifier is unnecessary for a single-file program. However, there is an exception.

The exception...

If ever you plan to use the class in a larger program, then adding a relevant package name would be essential.

This is because it would...

  1. Prevent name collisions when other classes in the default packages have the same name.

  2. Help people know whether or not your class is the one they want.

Can you imagine if 20 different developers made a List class in the default package, and somehow they all ended up in a project? It would be chaos! How would I choose the right List?

So in the case of writing a class that others will use in their own projects, you should definitely use package names.

byxor
  • 5,930
  • 4
  • 27
  • 44
0

It is probably non-production application if it has a single class and doesn't have any dependencies or resource files. So it is completely up to you how you will start your app.

If you want to distribute your app - make it in compliance with the standards, put it in a jar, publish to maven...

Denys Kurochkin
  • 1,360
  • 1
  • 18
  • 34
0

Java classloaders identify your classes by concatenating the package and the class name. So, if you don't use packages, the probabilities of name collisions are higher. Even though your app consists of only one class, you're going to reference many others, explicitly or not.

Also consider that you'll only be able to build very trivial applications with only one class, so probably that condition won't last forever.

Besides that, a class without package is a border case, so you'll probably find many tools that don't work with it.(I had this problem with the Web Service builder tool for Eclipse).

In short, use a package. It won't cause you any trouble, and (at least potentially) will save you many.

Andres
  • 10,561
  • 4
  • 45
  • 63