×

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose there is already a class named Rectangle in java.awt package, and we want to name a new class Rectangle in the graphics package. Although they are in distinct packages, the compiler permits both classes to share the same name. Each Rectangle class's fully qualified name contains the package name. That is, the Rectangle class in the graphics package has completely qualified name graphics.Rectangle, while the Rectangle class is in java.awt package has the fully qualified name java.awt.Rectangle. This works fine unless two separate programmers use the same name for their packages. This problem is prevented by convention.

Naming conventions

Package names are written in all lowercase to prevent confusion with the names of classes or interfaces.

Companies utilize their Internet domain name in reverse to start their package names, such as com.example.mypackage for a package written by a programmer at example.com with the name mypackage.

Within a single business, name clashes must be handled according to the convention. For example, the area or project name may be added after the company name (for example, com.example.region.mypackage).

The Java language itself has packages that start with java or javax.

The internet domain name might not always be a legal package name. This can happen if the package name starts with a character prohibited from being used as the first character in a Java name, such as a digit or the special character "int," or if the domain name contains a hyphen or another special character. The recommended practice in this situation is to include an underscore.

Legalizing Package Names

Domain NamePackage Name Prefix
hyphenated-name.example.orgorg.example.hyphenated_name
example.intint_.example
123name.example.comcom.example._123name

Using Package members

Package members are the several categories that make up a package.

You must perform one of the following actions to use a public package member from outside its package:

• Identify the member by their full legal name.

• Import, the package member

• Import, the whole package of the member

The sections that follow show how each is suitable for various circumstances.

Referring to a Package Member by Its Qualified Name

Since the beginning of this tutorial, most examples have referred to types by their brief names, like Rectangle and StackOfInts. If the code you are creating is in the same package as a package member or if the member has been imported, you can use the member's simple name.

However, you must use the member's fully qualified name, which incorporates the package name, if you are attempting to utilize a member from a separate package that has not yet been imported. The Rectangle class declared in the graphics package in the preceding example has the following fully qualified name.

graphics.Rectangle

You could use this qualified name to create an instance of graphics.Rectangle:

graphics.Rectangle myRect = new graphics.Rectangle();

For occasional use, qualified names are acceptable. However, if a name is typed repeatedly, it becomes tiresome, making the code more challenging to decipher. You can also import the member or its package and then use its short name as an alternative.

Importing a Package Member

Place an import statement at the beginning of the file, after the package statement (if present), but before any type declarations to import a specific member into the current file. Following is a description of importing the Rectangle class from the graphics package developed in the previous section.

import graphics.Rectangle;

The Rectangle class can now be referred to by its short name.

Rectangle myRectangle = new Rectangle();

This strategy works nicely if you only use a few members of the graphics package. However, you should import the whole package if you use many types from it.

Importing an Entire Package

Use the import statement with the asterisk (*) wildcard character to import every type found in a specific package.

Example

import graphics.*;

Any class or interface in the graphics package can now be referred to by its short name.

Example

Circle myCircle = new Circle();
Rectangle myRectangle = new Rectangle();

The asterisk in the import statement can only be used to specify every class in a package, as in this example. It cannot be used to match a portion of a package's classes. The following, for instance, does not correspond to all classes in the graphics package that start with A.

Example

// does not work
import graphics.A*;

Instead, a compiler error is produced. Using the import statement, you typically import just a single package member or a whole package.

Note: You can import the public nested classes of an enclosing class using a different, less frequent type of import. As an illustration, consider the case where the "graphics.Rectangle" class had valuable nested classes like "Rectangle.DoubleWide" and "Rectangle.Square”.

The following two statements could be used to import Rectangle and its nested classes.

import graphics.Rectangle;
import graphics.Rectangle.*;

Note: the second import statement won't import Rectangle, So keep that in mind.


Related Topics

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 minutes read.

Java.net.SocketException

Exception The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle...

4 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

How to avoid deadlock in java

Deadlock: A deadlock is an event that never going to occur. In java, deadlock is just a part of the multithreading. It is an environment that allows us to run multiple...

4 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

C# vs Java

Difference Between C# and Java C# and Java both languagesare popularly used programming languages. They both are derived from C/C++ programming and follow Object Oriented Programming approach. Even so, both these...

4 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.