I don't think it's enough to put it together, but ...
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 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.
[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