[Java] Development with multiple files using package and import

I don't think it's enough to put it together, but ...

Development with multiple files

When developing with multiple files, you can use the classes of other files with the ** import statement **. As the number of classes increases, organize them in ** packages **.

--ʻImport: Import by specifying the package and class. Described after package. --package`: Describe at the beginning of the sentence. Specify the package.

Here, the Main class and the Sub class are put in two packages, test.main and test.sub, respectively.

Main.java


package test.main;

import test.sub.Sub;

public class Main{
	public static void main(String[] args){
		Sub.hello();
	}
}

Sub.java


package test.sub;

public class Sub{
	public static void hello(){
		System.out.println("Hello World!");
	}
}

If you write Sub.hello () in detail in Main.java, it will be test.sub.Sub.hello (). Recognition that this is abbreviated by ʻimport`.

The structure of the folder is スクリーンショット 2019-04-24 15.49.31 2.png Feeling like that.

Make the current directory a folder called cd

java test.main.Main

You can use the Main class by typing.

Even if the working file is not in the current directory, if you specify the classpath at the time of the java command or register it in the OS in advance, the class loader will do the rest.

API# A large number of classes that come with Java in advance are called ** API (Application Programming Interface) **. (Although I say it in places other than java) In a typical place

--java.lang: Classes indispensable for Java. -- java.util: Make programming convenient. --java.math: Mathematics related. -- java.net: Network communication related --java.io: Involved in sequential processing of data such as reading and writing files.

When using

//Example 1
int r = new java.util.Random().nextInt(1);

//Example 2
Thread.sleep(3000);

How to write. this is,

--Example 1 --Package: java.util --Class; Random --Method: nextInt --Example 2 --Package: java.lang --Class: Thread --Method: sleep

You may recognize it like this. If you write the part Thread without abbreviation, it will be java.lang.Thread. The package java.lang appears frequently, so it can be abbreviated. In addition, nextInt (1) is a method that generates random numbers from 0 to 1, and sleep (3000) is a method that stops the program for 3 seconds.

Reference book

[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/dp/B00MIM1KFC/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1) Chapter 6 Pp.222-259

Recommended Posts

[Java] Development with multiple files using package and import
Html5 development with Java using TeaVM
[Java] Reading and writing files with OpenCSV
Game development with two people using java 2
Game development with two people using java 1
Game development with two people using java 3
[Java] Convert and import file values with OpenCSV
[Review] Reading and writing files with java (JDK6)
Switched property files for development and production using JMockit
Using Mapper with Java (Spring)
Using multiple versions of Java with Brew on Mac + jEnv
Problems with Dijkstra's algorithm using PriorityQueue and adjacency list (java)
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
Find the greatest common divisor and least common multiple with JAVA
Comparison of WEB application development with Rails and Java Servlet + JSP
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
Prepare Java development environment with Atom
Try using Redis with Java (jar)
Reading and writing Java basic files
Output PDF and TIFF with Java 8
Using Java with AWS Lambda-Eclipse Preparation
Drag and drop files with JavaFX
Deleting files using recursive processing [Java]
Notes on Java path and Package
Using proxy service with Java crawling
Encrypt with Java and decrypt with C #
Using Java with AWS Lambda-Implementation Tips-Get Instance Name from Reagion and Instance ID
Notes for reading and generating xlsx files from Java using Apache POI
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
Monitor Java applications with jolokia and hawtio
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
Link Java and C ++ code with SWIG
Using JupyterLab + Java with WSL on Windows 10
Sort by multiple conditions using Java Stream
Let's try WebSocket with Java and javascript!
[Java] Handle Excel files with Apache POI
[Java] How to output and write files!
What is Java and Development Environment (MAC)
Delete folders and files with File Manager
I tried using OpenCV with Java + Tomcat
Save files and folders using File Manager
[Kotlin] Delete files with duplicate contents [Java]
Summary of Java Math.random and import (Calendar)
Java development with Codenvy: Console app debug
Try using the Wii remote with Java
24 hours struggling with Android development, Java Optional
Reading and writing gzip files in Java
Be careful with requests and responses when using the Serverless Framework in Java
Interact with LINE Message API using Lambda (Java)
Create API using Retrofit2, Okhttp3 and Gson (Java)
Install java and android-sdk on Mac using homebrew
Streamlining Web system development using docker-compose and Git
Java Development Basics ~ Development Environment Settings and Project Creation ~
Build and test Java + Gradle applications with Wercker
Try to link Ruby and Java with Dapr
JSON with Java and Jackson Part 2 XSS measures
Specify ClassPath when using jupyter + Java with WSL