Hướng dẫn package diagram

What is Package in Java?

PACKAGE in Java is a collection of classes, sub-packages, and interfaces. It helps organize your classes inkhổng lồ a thư mục structure and make it easy lớn locate và use them. More importantly, it helps improve code reusability.


Bạn đang xem: Hướng dẫn package diagram

Each package in Java has its quality name and organizes its classes & interfaces inkhổng lồ a separate namespace, or name group. Although interfaces và 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 khổng lồ each Java package. Syntax:- package nameOfPackage;The following đoạn Clip takes you through the steps of creating a package.

Let"s study package with an example. We define a class và object and later compile this it in our package p1. After compilation, we exedễ thương the code as a java package.


Xem thêm: Hướng Dẫn Cách Ghép 2 Tấm Hình Thành 1 Cực Kỳ Dễ Dàng, Nhanh Chóng

How lớn Create a package?

Creating a package is a simple task as follows Choose the name of the package Include the package commvà 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 lớn create the Java packages


Xem thêm: Hướng Dẫn Cách Làm Địu Vải Cho Bé, Cách Làm Địu Vải Cho Bé

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();Creating & Using package in Java Here, To put a class into lớn a package, at the first line of code define package p1Create a class c1Defining a method m1 which prints a line.Defining the main methodCreating an object of class c1Calling method m1Step 2) In next step, save sầu this tệp tin as demo.java Creating & Using package in Java Creating và Using package in Java Step 3) In this step, we compile the file. Creating and Using package in Java The compilation is completed. A class tệp tin c1 is created. However, no package is created? Next step has the solution Creating & Using package in Java Step 4) Now we have sầu to create a package, use the commvà javac –d . kiểm tra.javaThis comm& forces the compiler to lớn create a package. The "." operator represents the current working directory. Creating và Using package in Java Step 5) When you exexinh tươi the code, it creates a package p1. When you open the java package p1 inside you will see the c1.class file. Creating & Using package in Java Step 6) Compile the same file using the following code javac –d .. kiểm tra.javaHere ".." indicates the parent directory. In our case tệp tin will be saved in parent directory which is C Drive sầu Creating và Using package in Java File saved in parent directory when above sầu code is executed. Creating and Using package in Java Step 7) Now let"s say you want lớn create a sub package p2 within our existing java package p1. Then we will modify our code as package p1.p2;class c1public void m1() System.out.println("m1 of c1");Creating và Using package in Java Step 8) Compile the file Creating và Using package in Java As seen in below screenshot, it creates a sub-package p2 having class c1 inside the package. Creating & Using package in Java Step 9) To exedễ thương 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 - java p1.p2.c1Creating & Using package in Java This is how the package is executed & gives the output as "m1 of c1" from the code tệp tin. Creating & Using package in Java

How to lớn Import Package

To create an object of a class (bundled in a package), in your code, you have sầu khổng lồ use its fully qualified name. Example: 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 khổng lồ use. Instead, it is recommended you use the import statement. Syntax import packageName;Once imported, you can use the class without mentioning its fully qualified name. import java.awt.event.*; // * signifies all classes in this package are importedimport javax.swing.JFrame // here only the JFrame class is imported//UsageJFrame f = new JFrame; // without fully qualified name.Example: To import package
Step 1) Copy the code inkhổng lồ an editor. package p3;import p1.*; //imports classes only in package p1 and NOT in the sub-package p2class 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 tệp tin using the comm& javac –d . Demo2.javaStep 3)Exeđáng yêu the code using the commvà java p3.c3

Packages - points to lớn note:

To avoid naming conflicts packages are given names of the domain name of the company in reverse Ex: com.autocadtfesvb.com. 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 lớn exeđáng yêu 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 allowedimport package p1.*;package p3;//correct syntaxpackage p3;import package p1.*;the java.lang package is imported by mặc định for any class that you create in Java. The Java API is very extensive sầu, contains classes which can perkhung almost all your programming tasks right from Data Structure Manipulation khổng lồ Networking. More often than not, you will be using API files in your code. You can see the API documentation here.


Chuyên mục: kiến thức