You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*[512](https://openjdk.org/jeps/512) - Compact Source Files and Instance Main Methods
11
+
12
+
## Features
13
+
14
+
***Module Import Declarations**
15
+
* promotion to standard without change
16
+
* allow import all packages exported by a module
17
+
* goal is simplify the learning and exploring of APIs without having to know its exactly package
18
+
* when importing a module, it will automatically import all its exported packages and its transitive dependencies
19
+
* import module `java.se` will import the entire Java SE API
20
+
* syntax:
21
+
*`import module [Module Name]`
22
+
*`import module java.sql`
23
+
* ambiguous import:
24
+
* in case of two module exporting the same class name, we can import the class directly or its package
25
+
* allow any type-import-on-demand declarations to shadow module import declarations
26
+
*
27
+
```java
28
+
// module imports
29
+
importmodulejava.base; // java.util.Date
30
+
importmodulejava.sql; // java.sql.Date
31
+
32
+
// package imports
33
+
importjava.util.*; // every class used will shadow the class from module imports
34
+
35
+
// single-type imports
36
+
importjava.sql.Date; // will shadow all above imports
37
+
```
38
+
***CompactSourceFiles and InstanceMainMethods**
39
+
* promotion to standard with minor changes
40
+
* changed terminology from "simple source file" to "compact source file"
41
+
* changed package of `IO` to `java.lang.IO`
42
+
* facilitate the writing of first programm for students without needing to know another features designed for large programs
43
+
* implicitly declared class:
44
+
* any method declared in a source file without an enclosed classwill be considered to be member of an implicitly declared classwhose members are unenclosed fields and methods
45
+
* implicitly declared classis always final and cannot implement or extends any class other than `Object`
46
+
* the compiler requires an implicitly declared method to have a launchable main method
47
+
* we cannot use javadoc tool to generate documation from a implicitly declared class (doesn't have a accessible API from other class)
48
+
* is equivalent to the following usage of anonymous class declaration:
49
+
```java
50
+
new Object() {
51
+
voidmain() {}
52
+
}.main();
53
+
```
54
+
* as it doesn't have a name, so we cannot use its static methods using its class name (`ClassName.staticMethodName()`)
55
+
* it still will have the impliticy `this` reference
56
+
* it can have:
57
+
* instance and static methods
58
+
* instance and static fields
59
+
* the default modifers are the same (package access and instance membership)
60
+
* the main difference is will only have an impliticy default zero-paramater constructor and cannot have initializers (static nor instance)
61
+
* implicitly class will automatically import all of the public top-level classes and interfaces from module `java.base`
62
+
* `Class.isSynthetic` method returns true
63
+
* launchable main method:
64
+
* change to how Java programs are launched
65
+
* instance main method:
66
+
* the class must have a non-private zero-paramater constructor
67
+
* the main method doesn't need to be static, public nor have parameter
68
+
* example:
69
+
```java
70
+
classHelloWord {
71
+
voidmain() {
72
+
System.out.println("Hello World!");
73
+
}
74
+
}
75
+
```
76
+
* allowing implicitly declared class:
77
+
```java
78
+
void main() {
79
+
System.out.println("Hello World!");
80
+
}
81
+
```
82
+
* order to select a main method (must be non-private and it prioritize the methods in current classbefore the superclass):
83
+
* `static void main(String[])`
84
+
* `void main(String[])`
85
+
* `static void main()`
86
+
* `void main()`
87
+
* `java.lang.IO`
88
+
* new classcreated for convenience I/O
89
+
* provides methods: `print(Object)`, `println(Object)`, `println()`, `readln(String)` and `readln()`
90
+
* this classuses return from `System.console()` to print and read from the default input and output streams
0 commit comments