×

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 Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

Virtual Function in Java

In the Operated Oriented Programming language, a virtual function or virtual method is a collection of functions that overrides the functionality of a function in an inheriting class with the same...

4 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 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.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.