Java import org package

Java Package

A package is simply a container that groups related types (Java classes, interfaces, enumerations, and annotations). For example, in core Java, the ResultSet interface belongs to the java.sql package. The package contains all the related types that are needed for the SQL query and database connection.

If you want to use the ResultSet interface in your code, just import the java.sql package (Importing packages will be discussed later in the article).

As mentioned earlier, packages are just containers for Java classes, interfaces and so on. These packages help you to reserve the class namespace and create a maintainable code.

For example, you can find two Date classes in Java. However, the rule of thumb in Java programming is that only one unique class name is allowed in a Java project.

How did they manage to include two classes with the same name Date in JDK?

This was possible because these two Date classes belong to two different packages:

  • java.util.Date — this is a normal Date class that can be used anywhere.
  • java.sql.Date — this is a SQL Date used for the SQL query and such.

Based on whether the package is defined by the user or not, packages are divided into two categories:

Built-in Package

Built-in packages are existing java packages that come along with the JDK. For example, java.lang , java.util , java.io , etc. For example:

import java.util.ArrayList; class ArrayListUtilization < public static void main(String[] args) < ArrayListmyList = new ArrayList<>(3); myList.add(3); myList.add(2); myList.add(1); System.out.println(myList); > > 

The ArrayList class belongs to java.util package . To use it, we have to import the package first using the import statement.

User-defined Package

Java also allows you to create packages as per your need. These packages are called user-defined packages.

How to define a Java package?

To define a package in Java, you use the keyword package .

Java uses file system directories to store packages. Let’s create a Java file inside another directory.

Now, edit Test.java file, and at the beginning of the file, write the package statement as:

Here, any class that is declared within the test directory belongs to the com.test package.

package com.test; class Test < public static void main(String[] args)< System.out.println("Hello World!"); >>

Here, the Test class now belongs to the com.test package.

Package Naming convention

The package name must be unique (like a domain name). Hence, there’s a convention to create a package as a domain name, but in reverse order. For example, com.company.name

Читайте также:  Php date for yesterday

Here, each level of the package is a directory in your file system. Like this:

And, there is no limitation on how many subdirectories (package hierarchy) you can create.

How to create a package in Intellij IDEA?

In IntelliJ IDEA, here’s how you can create a package:

  1. Right-click on the source folder.
  2. Go to new and then package. New package in Intellij IDEA
  3. A pop-up box will appear where you can enter the package name. Package Naming convention in Java

Once the package is created, a similar folder structure will be created on your file system as well. Now, you can create classes, interfaces, and so on inside the package.

Package directory

How to import packages in Java?

Java has an import statement that allows you to import an entire package (as in earlier examples), or use only certain classes and interfaces defined in the package.

The general form of import statement is:

import package.name.ClassName; // To import a certain class only import package.name.* // To import the whole package
import java.util.Date; // imports only Date class import java.io.*; // imports everything inside java.io package

The import statement is optional in Java.

If you want to use class/interface from a certain package, you can also use its fully qualified name, which includes its full package hierarchy.

Here is an example to import a package using the import statement.

import java.util.Date; class MyClass implements Date < // body >

The same task can be done using the fully qualified name as follows:

class MyClass implements java.util.Date < //body >

Example: Package and importing package

Suppose, you have defined a package com.programiz that contains a class Helper .

package com.programiz; public class Helper < public static String getFormattedDollar (double value)< return String.format("$%.2f", value); >>

Now, you can import Helper class from com.programiz package to your implementation class. Once you import it, the class can be referred directly by its name. Here’s how:

import com.programiz.Helper; class UseHelper < public static void main(String[] args) < double value = 99.5; String formattedValue = Helper.getFormattedDollar(value); System.out.println("formattedValue Importing packages in Java" height="177" src="https://cdn.programiz.com/sites/tutorial2program/files/import-package-java_0.jpg" title="Java import package" width="400">
Java import package

In Java, the import statement is written directly after the package statement (if it exists) and before the class definition.

For example,

package package.name; import package.ClassName; // only import a Class class MyClass < // body >

Table of Contents

Источник

Java Packages – How to use them

We look at java packages in this article. If you want to learn java, java packages topic will be a great start. We look at code samples to present how to use the java packages.

2. Java Packages

Java packages help in avoiding naming collisions, access control, and separation of modules.They help in making usage of interfaces, annotations, and classes simpler. It helps in grouping a set of classes and related types. It is easier to locate and search by package namespace. In java, we use java.lang and java.io for writing basic code. They help in bundling modules and breaking down a complex system to a group of packages. There are built-in packages such as java.lang , javax.swing , java.sql , java.net , java.awt , and java.util in java sdk.

Java Packages

2.1 Prerequisites

Java 8 is required on the Linux, Windows or Mac operating system. Eclipse Oxygen can be used for this example.

2.2 Download

You can download Java 8 from the Oracle web site . Eclipse Oxygen can be downloaded from the eclipse web site.

2.3 Setup

2.3.1 Java Setup

Below are the setup commands required for the Java Environment.

JAVA_HOME=”/jboss/jdk1.8.0_73″ export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH

2.4 IDE

2.4.1 Eclipse Oxygen Setup

The ‘eclipse-java-oxygen-2-macosx-cocoa-x86_64.tar’ can be downloaded from the eclipse website. The tar file is opened by double click. The tar file is unzipped by using the archive utility. After unzipping, you will find the eclipse icon in the folder. You can move the eclipse icon from the folder to applications by dragging the icon.

2.4.2 Launching IDE

Eclipse has features related to language support, customization, and extension. You can click on the eclipse icon to launch eclipse. The eclipse screen pops up as shown in the screenshot below:

Java Packages - Launching IDE

Java Packages - IntelliJ vs Eclipse

You can select the workspace from the screen which pops up. The attached image shows how it can be selected.

Java Packages - Eclipse Workbench

You can see the eclipse workbench on the screen. The attached screenshot shows the Eclipse project screen.

Java Packages - Java Hello

Java Hello World class prints the greetings. The screenshot below is added to show the class and execution on the eclipse.

2.5 What is a java package?

A java package is a group of classes, interfaces, enumerations, and annotations. Package keyword is used to declare the namespace of class, interface enumeration and annotation. Below is the code which defines a package by name “ org.java.examples ” and a class SimpleExample . Package Keyword

/** * */ package org.java.examples; /** * @author bhagvan.kommadi * */ public class SimpleExample < /** * @param args */ public static void main(String[] args) < // TODO Auto-generated method stub System.out.println("Printing Java Package Examples"); >>

Java Packages - Simple Package

The output of the code when you run it in eclipse is shown in the screenshot below:

Default package is taken when the package name is not specified. Package statement needs to be mentioned before import statements in a class, interface, enumeration, and annotation. The sample code is shown below for a DefaultExample class with no package definition. Default Package

/** * @author bhagvan.kommadi * */ public class DefaultExample < /** * @param args */ public static void main(String[] args) < System.out.println("Printing Default Java Package Example"); >>

Java Packages - Default Package

The output of the above code, when executed in eclipse, is shown in the screenshot below.

2.6 How to import a java package

Importing a java package is done using the keyword import. In the example below, SimpleExample class is imported using the keyword import. It is imported as org.java.examples.SimpleExample . Importing a java package

package org.java.services; import org.java.examples.SimpleExample; /** * @author bhagvan.kommadi * */ public class UsePackageExample < /** * @param args */ public static void main(String[] args) < SimpleExample example = new SimpleExample(); example.print(); >>

The output of the above code, when executed in eclipse is shown in the screenshot below.

2.6.1 Static Import

Let us look at how static import is used. A static import helps in accesses the class static members without mentioning the class name statically. Sample code below shows how to use static import of “ java.lang.System.out ” class. Static Import

package org.java.examples; import static java.lang.System.out; /** * @author bhagvan.kommadi * */ public class StaticExample < /** * @param args */ public static void main(String[] args) < out.println("Inside Static Import Example"); >>

In the screenshot below, the output of code when executed in eclipse is shown.

2.6.2 Command-Line Options

You can use commands in the terminal to get the package structure. Packages are closely related to the directories. “tree” command can be used to show the package structure. The output of the command tree is presented in the screenshot below.

You can use javac -d option to compile a Java class and place the compiled class in the package folder. Command Line Option

mkdir Test javac -d test Test.java

The output of the commands after execution is shown in the screenshot below.

2.7 How to create your own package in Eclipse

In eclipse, you can create a package from the menu after creating a java project. The menu that you need to select will be -> New Package. The screenshot below shows the menu navigation.

Next screen, you need to input the name of the package. The screenshot below shows the screen.

You can name the package as org.java.examples . The screenshot below shows the input screen.

The project gets updated with a package created. The screenshot below shows the final result.

2.8 Package name conventions

Java packages are named in lower case to avoid conflict with names of interfaces, classes, enumerations, and annotations. Commercial software packages are named using “com” prefix. Opensource software packages are named using the “org” prefix. Java sdk and api use “java” prefix. Java package should not start with a digit or non-alphabetic character as per java name convention.

2.9 Package Access

Default access for the classes in a package is package level access. The default access modifier for a class is used to mention that this class cannot be used outside the package. Sample code is shown below : Package Access

/** * @author bhagvan.kommadi * */ public class DefaultExample < /** * @param args */ public static void main(String[] args) < System.out.println("Printing Default Java Package Example"); >>

2.10 Package Class

The package class has methods to find about the specification and implementation of a java package. It has methods such as getName() , getImplementationTitle() , getImplementationVendor() , getImplementationVersion() etc. Package Metadata

package org.java.examples; /** * @author bhagvan.kommadi * */ public class PackageMetaDataExample < /** * @param args */ public static void main(String[] args) < // TODO Auto-generated method stub Package pack=Package.getPackage("java.lang"); System.out.println("Package - "+pack.getName()); System.out.println(" Package Specification Title - "+pack.getSpecificationTitle()); System.out.println("Package Specification Vendor - "+pack.getSpecificationVendor()); System.out.println("Package Specification Version - "+pack.getSpecificationVersion()); System.out.println("Package Implementaion Title - "+pack.getImplementationTitle()); System.out.println("Package Implementation Vendor - "+pack.getImplementationVendor()); System.out.println(" Package Implementation Version - "+pack.getImplementationVersion()); System.out.println("Package Check If sealed - "+pack.isSealed()); >>

You can use Package class to get the metadata of the packages which are loaded from the classpath. The output of the code, when executed in eclipse, is shown in the screenshot below.

Источник

Оцените статью