[Java] Package for management

When programming in Java, if you create a lot of classes to be created, it will be difficult to manage unless you organize them properly. Also, if you have created a class jointly, you need to distinguish it from the class you created when you use a class created by another person. Describes the package for doing that.

Package and class

In Java, you can collect classes to create a collection of classes called a package. If you think of a class as a part, you can say that a collection of strongly related parts is a ** package **. For example, when creating a program that performs scientific calculations, it is not necessary to describe complicated calculation formulas from scratch in the program, and scientific calculations can be easily performed by obtaining a scientific calculation package and using it. I will. Each package has a unique name to distinguish it from other packages. Java recommends that you put the reverse order of your Internet domain at the beginning of your package to ensure that your unique naming is followed. For example, if the domain name is "blogramming.co.jp", the package name will start with "jp.co.blogramming".

Use of packages using package names

Java is designed to work on the packages you need, when you need them, and as much as you need them. Therefore, it is necessary to specify which package to import by using import in the program. One way to do this is to use a package with a specified package name. The program shown below uses the DecimalFormat class of the java.text package. By adding "." After the package name and specifying the class name, an instance of the DecimalFormat class included in the java.text package is created.

PackageTest1.java


class PackageTest1{
  public static void main(String[] args){
    int x=1234567;

    java.text.DecimalFormat df=new java.text.DecimalFormat(",###");

    System.out.println("x="+x);
    System.out.println("x="+df.format(x));
  }
}

Package use by import

By writing the package name, it is possible to use the classes included in the package, but the program description has become very long. Use import here. By using import, the package can be imported, so it is possible to use it as a class at hand with only the class name without describing the package name. However, import does not physically copy the class in the package or insert it in the program list, but logically imports it, so once you import it, you do not have to do it later. Instead, you should always write import while writing the program.

PackageTest2.java


import java.text.DecimalFormat;

class PackageTest2{
  public static void main(String[] args){
    int x=1234567;

    DecimalFormat df=new DecimalFormat(",###");

    System.out.println("x="+x);
    System.out.println("x="+df.format(x));
  }
}

Recommended Posts

[Java] Package for management
For JAVA learning (2018-03-16-01)
2017 IDE for Java
Java for statement
[Java] for statement, while statement
[Java] for statement / extended for statement
Countermeasures for Java OutOfMemoryError
NLP for Java (NLP4J) (2)
(Memo) Java for statement
NLP for Java (NLP4J) (1)
Java: Timed token management class for Web API authentication
Java update for Scala users
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
[Java] Precautions for type conversion
Books used for learning Java
2018 Java Proficiency Test for Newcomers-Basics-
Java thread safe for you
[Java] Summary of for statements
Java for beginners, data hiding
[Java] Tips for writing source
Java installation location for mac
Java application for beginners: stream
Java while and for statements
C # cheat sheet for Java engineers
New grammar for Java 12 Switch statements
[For beginners] Summary of java constructor
AWS SDK for Java 1.11.x and 2.x
Rock-paper-scissors game for beginners in Java
Java for beginners, expressions and operators 1
[Java] Memo for naming class names
Summary of [Java silver study] package
[For beginners] Run Selenium in Java
Hello World for ImageJ Java Plugin
[OpenCV3.2.0] Eclipse (Java) settings (for Mac)
Java for beginners, expressions and operators 2
Enable OpenCV with java8. (For myself)
Spring Framework tools for Java developer
java (use class type for field)
Build Java development environment (for Mac)
Java
[Java] Spring DI ④ --Life cycle management
[Java & SpringBoot] Environment Construction for Mac
Settings for SSL debugging in Java
Generics of Kotlin for Java developers
Java
Diary for Java SE 8 Silver qualification
[For Java beginners] About exception handling
Notes on Java path and Package
Classes and instances Java for beginners
Modern best practices for Java testing
GraalVM for Java Performance (Windows Developer Build)
[Until March 5, 2020] Renew RDS certificate for java
Getting Started with Ruby for Java Engineers
[Java Spring MVC] Controller for development confirmation
Memory measurement for Java apps using jstat
Introduction to java for the first time # 2
Studying Java 8 (date API in java.time package)
First steps for deep learning in Java
Java for All! I read everyone's Java #minjava
I tried Cassandra's Object Mapper for Java