Summary of [Java silver study] package

About the package

What is a package?

A mechanism for organizing classes. There are many classes in the Java language. Therefore, if a large number of classes are organized by purpose and function, it will be easier to use for each purpose and class management will be easier.

How to declare a package

Declare with "package package name;". (Correct) Describe in the first line of the source code

Hello.java


package jp.co.xxx;
import aaa.*;
public class Hello(){
  System.out.println("hello world!");
}

(Error) import is described before the package declaration

Hello.java


import aaa.*;
package jp.co.xxx;
public class Hello(){
  System.out.println("hello world!");
}

Must be written on the first line of the source code Only comments can be written before the package declaration

Purpose

Provide namespaces and avoid name collisions

If the package is different, even a class with the same class name will be a different class. If there are classes with the same name, the compiler and JVM cannot determine which class to use, which may result in a compilation error or the intended class not being used. In that case, use it to avoid duplicate names. The compiler and JVM judge the class by "package name.class name". Therefore, it is desirable that the package name is as unique as possible. By the way, the expression in "package name.class name" is called a fully qualified class name, and the abbreviation for "class name" is called a simple name.

Fully qualified class name: "java.lang.String" Simple name: "String"

Provide access control function

By dividing the class into multiple packages, access control can be performed on a package-by-package basis. By dividing the class in the package into a public class and a non-public class, it is possible to prevent the class different from the expected class from being used. ** Access modifier type ** "Private": Only accessible from within the same class "Protected": Classes in the same package or different packages can be accessed from within a subclass that inherits that class. "Public": accessible from all classes None: accessible from all classes in the same package

Declaration of public Hello class belonging to jp.co.xxx package

Hello.java


package jp.co.xxx;
public class Hello(){
  System.out.println("hello world!");
}

Declaration of Bye class belonging to jp.co.xxx package

Bye.java


package jp.co.xxx;
class Bye(){
  System.out.println("Bye!");
}

The public Hello class can be used by classes belonging to other packages, but the non-public Bye class cannot be used.

Allows class classification

The package has a directory structure. The directories under the source directory are packages. If it is a "jp.co.xxx.Hello" class, it will be like "\ jp \ co \ xxx \ Hello.class". Classes always belong to some package, and classes that omit the package declaration are interpreted as belonging to an anonymous package by default. There is no class that does not belong to the package.

Recommended Posts

Summary of [Java silver study] package
Study Java Silver 1
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
Java Silver Study Day 1
Summary of Java support 2018
[Java11] Stream Summary -Advantages of Stream-
Java Silver Study Method Memo
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[Java Silver] Summary of points related to lambda expressions
Summary of knowledge required to pass Java SE8 Silver
How to study Java Silver SE 8
[For beginners] Summary of java constructor
Summary of object-oriented programming using Java
[java] Summary of how to handle char
Summary of changes other than JEP of Java10
[Java] Personal summary of conditional statements (basic)
[Java] [Maven3] Summary of how to use Maven3
Java Summary of frequently searched type conversions
Summary of Java Math.random and import (Calendar)
Java knowledge summary
Java Generics Summary
Let's study Java
Java Silver memo
Java related summary
Java 8 documentation summary
[Java] Study notes
Java 8 study (repeatable)
Java study memorandum
Java 11 document summary
[Java] Overview of Java
Summary of Java environment settings for myself [mac]
[Java] Personal summary of classes and methods (basic)
[Java] Summary of how to abbreviate lambda expressions
[Java SE 11 Silver] Arrays class method summary [Java beginner]
[Qualification Exam] Java SE 8 Silver Learning Method Summary
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Expired collection of java
Predicted Features of Java
Java 12 new feature summary
Passed Java SE8 Silver
This is convenient! Summary of popular Java library + α
[Java] Significance of serialVersionUID
Omission of curly braces in if statement (Java silver)
java bronze silver passed
Summary of ToString behavior with Java and Groovy annotations
[Summary] Java environment preparation
effective java 3rd summary
[Java] Package for management
NIO.2 review of java
Review of java Shilber
Java 13 new feature summary
Java Silver passing experience