Arranging Classes by Functionality

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn to use packages to arrange for classes according to their functionalities.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/inner_classes("`Inner Classes`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/inner_classes -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/classes_objects -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/class_methods -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/modifiers -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/oop -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/packages_api -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/identifier -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/arrays -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/comments -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/data_types -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/operators -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/output -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/strings -.-> lab-178550{{"`Arranging Classes by Functionality`"}} java/system_methods -.-> lab-178550{{"`Arranging Classes by Functionality`"}} end

Packages

Till now, we have written many classes. For a big project, maybe there will be hundreds of classes, so many files and classes are not easy to organize, different programmers may write different classes, sometimes it would happen that classes with same name appear in a project. So how to avoid these problems, we can use packages. The Java libraries are divided into packages, including java.lang, which contains most of the classes we have used so far. 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.

To create a package, choose a name for the package and put a package statement with that name at the top of every source file that contains the types that you want to include in the package.The package statement must be the first line in the source file. Only one package statement can be in each source file.

To use a public package member from outside its package, you must do one of the following:

  • Refer to the member by its fully qualified name
  • Import the package member
  • Import the member's entire package

Example

Write the following code in the /home/labex/project/com/example/demo/packageDemo.java file:

package com.example.demo;  // create a new package

public class packageDemo{
    public static void test(){
        System.out.println("This is a package test.");
    }
}

Write the following code in the /home/labex/project/packageTest.java file:

import com.example.demo.packageDemo;
class packageTest{
    public static void main(String[] args){
        packageDemo.test();
    }
}

Output:

Run the packageTest.java file using the following commands:

javac /home/labex/project/packageTest.java
java packageTest

See the output:

This is a package test.

Summary

Packages provide us a way to collect classes which have logical relation or achieve some special functions, using packages, we can get avoid name conflict. You can pack your code into packages providing to others or get packages from others, on the internet. It provides us more convenience of coding.

Other Java Tutorials you may like