Java как сделать package

Packages in Java: How to Create/Import Package

PACKAGE in Java is a collection of classes, sub-packages, and interfaces. It helps organize your classes into a folder structure and make it easy to locate and use them. More importantly, it helps improve code reusability.

Each package in Java has its unique name and organizes its classes and interfaces into a separate namespace, or name group.

Although interfaces and classes with the same name cannot appear in the same package, they can appear in different packages. This is possible by assigning a separate namespace to each Java package.

The following video takes you through the steps of creating a package.

Click here if the video is not accessible

Let’s study package with an example. We define a class and object and later compile this it in our package p1. After compilation, we execute the code as a java package.

How to Create a package?

Creating a package is a simple task as follows

  • Choose the name of the package
  • Include the package command as the first line of code in your Java Source File.
  • The Source file contains the classes, interfaces, etc you want to include in the package
  • Compile to create the Java packages

Step 1) Consider the following package program in Java:

package p1; class c1() < public void m1()< System.out.println("m1 of c1"); >public static void main(string args[]) < c1 obj = new c1(); obj.m1(); >>

How to Create a package

  1. To put a class into a package, at the first line of code define package p1
  2. Create a class c1
  3. Defining a method m1 which prints a line.
  4. Defining the main method
  5. Creating an object of class c1
  6. Calling method m1

Step 2) In next step, save this file as demo.java

How to Create a package

How to Create a package

Step 3) In this step, we compile the file.

How to Create a package

The compilation is completed. A class file c1 is created. However, no package is created? Next step has the solution

How to Create a package

This command forces the compiler to create a package.

The “.” operator represents the current working directory.

How to Create a package

Step 5) When you execute the code, it creates a package p1. When you open the java package p1 inside you will see the c1.class file.

How to Create a package

Step 6) Compile the same file using the following code

Читайте также:  Filereader java чтение построчно

Here “..” indicates the parent directory. In our case file will be saved in parent directory which is C Drive

How to Create a package

File saved in parent directory when above code is executed.

How to Create a package

Step 7) Now let’s say you want to create a sub package p2 within our existing java package p1. Then we will modify our code as

How to Create a package

Step 8) Compile the file

How to Create a package

As seen in below screenshot, it creates a sub-package p2 having class c1 inside the package.

How to Create a package

Step 9) To execute the code mention the fully qualified name of the class i.e. the package name followed by the sub-package name followed by the class name –

How to Create a package

This is how the package is executed and gives the output as “m1 of c1” from the code file.

How to Create a package

How to Import Package

To create an object of a class (bundled in a package), in your code, you have to use its fully qualified name.

java.awt.event.actionListner object = new java.awt.event.actionListner();

But, it could become tedious to type the long dot-separated package path name for every class you want to use. Instead, it is recommended you use the import statement.

Once imported, you can use the class without mentioning its fully qualified name.

import java.awt.event.*; // * signifies all classes in this package are imported import javax.swing.JFrame // here only the JFrame class is imported //Usage JFrame f = new JFrame; // without fully qualified name.

Step 1) Copy the code into an editor.

package p3; import p1.*; //imports classes only in package p1 and NOT in the sub-package p2 class c3 < public void m3()< System.out.println("Method m3 of Class c3"); >public static void main(String args[]) < c1 obj1 = new c1(); obj1.m1(); >>

Step 2) Save the file as Demo2.java. Compile the file using the command javac –d . Demo2.java

Step 3)Execute the code using the command java p3.c3

Packages – points to note:

  • To avoid naming conflicts packages are given names of the domain name of the company in reverse Ex: com.guru99. com.microsoft, com.infosys etc.
  • When a package name is not specified, a class is in the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
  • While creating a package, care should be taken that the statement for creating package must be written before any other import statements
// not allowed import package p1.*; package p3; //correct syntax package p3; import package p1.*;

the java.lang package is imported by default for any class that you create in Java.

Читайте также:  Enable cache in php

The Java API is very extensive, contains classes which can perform almost all your programming tasks right from Data Structure Manipulation to Networking. More often than not, you will be using API files in your code. You can see the API documentation here.

Источник

Creating and Using Packages

To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages.

Definition: A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this lesson simply as classes and interfaces.

The types that are part of the Java platform are members of various packages that bundle classes by function: fundamental classes are in java.lang , classes for reading and writing (input and output) are in java.io , and so on. You can put your types in packages too.

Suppose you write a group of classes that represent graphic objects, such as circles, rectangles, lines, and points. You also write an interface, Draggable , that classes implement if they can be dragged with the mouse.

//in the Draggable.java file public interface Draggable < . >//in the Graphic.java file public abstract class Graphic < . >//in the Circle.java file public class Circle extends Graphic implements Draggable < . . . >//in the Rectangle.java file public class Rectangle extends Graphic implements Draggable < . . . >//in the Point.java file public class Point extends Graphic implements Draggable < . . . >//in the Line.java file public class Line extends Graphic implements Draggable

You should bundle these classes and the interface in a package for several reasons, including the following:

  • You and other programmers can easily determine that these types are related.
  • You and other programmers know where to find types that can provide graphics-related functions.
  • The names of your types won’t conflict with the type names in other packages because the package creates a new namespace.
  • You can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package.
Читайте также:  Exception in thread awt eventqueue 0 java lang nullpointerexception cannot invoke

Источник

Creating a Package

To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.

The package statement (for example, package graphics; ) must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

Note: If you put multiple types in a single source file, only one can be public , and it must have the same name as the source file. For example, you can define public class Circle in the file Circle.java , define public interface Draggable in the file Draggable.java , define public enum Day in the file Day.java , and so forth.

You can include non-public types in the same file as a public type (this is strongly discouraged, unless the non-public types are small and closely related to the public type), but only the public type will be accessible from outside of the package. All the top-level, non-public types will be package private.

If you put the graphics interface and classes listed in the preceding section in a package called graphics , you would need six source files, like this:

//in the Draggable.java file package graphics; public interface Draggable < . . . >//in the Graphic.java file package graphics; public abstract class Graphic < . . . >//in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable < . . . >//in the Rectangle.java file package graphics; public class Rectangle extends Graphic implements Draggable < . . . >//in the Point.java file package graphics; public class Point extends Graphic implements Draggable < . . . >//in the Line.java file package graphics; public class Line extends Graphic implements Draggable

If you do not use a package statement, your type ends up in an unnamed package. Generally speaking, an unnamed package is only for small or temporary applications or when you are just beginning the development process. Otherwise, classes and interfaces belong in named packages.

Источник

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