Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of defining a package in a Java file? [closed]

Tags:

java

package

I am a newbie and just learned that if I define say

package my.first.group.here; ... 

then the Java files that are in this package will be placed under my/first/group/here directory.

What is the main purpose of putting some Java files in a package? Also, if I choose to adopt this, how should I group them?

Thank you


EDIT: For anyone who might have the same question again, I just found this tutorial on packages from Sun.

like image 790
derrdji Avatar asked Jul 06 '09 18:07

derrdji


People also ask

Why do we define package in Java?

A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code.

What is the purpose of using packages?

Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.

What is the purpose of a package explain the steps to create user define packages in Java?

Packages in Java are a mechanism to encapsulate a group of classes, interfaces, and sub-packages. In Java, it is used for making search/locating and usage of classes, interfaces, enumerations, and annotations easier. It can be considered data encapsulation also.


2 Answers

Let's start with the definition of a "Java package", as described in the Wikipedia article:

A Java package is a mechanism for organizing Java classes into namespaces similar to the modules of Modula. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.

So based on that, packages in Java are simply a mechanism used to organize classes and prevent class name collisions. You can name them anything you wish, but Sun has published some naming conventions that you should use when naming packages:

Packages

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

Examples:

  • com.sun.eng

  • com.apple.quicktime.v2

  • edu.cmu.cs.bovik.cheese

like image 99
William Brendel Avatar answered Oct 06 '22 00:10

William Brendel


I a large application, you are bound to have two files named exactly the same (java.util.Date and java.sql.Date), especially when you start bringing in third party jars. So basically, you can use packages to ensure uniqueness.

Most importantly, in my opinion, packaging breaks down projects into meaningful segments. So my SQL package has sql-related code, and my logger package handles logging.

like image 40
geowa4 Avatar answered Oct 06 '22 00:10

geowa4