-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathessay20.txt
36 lines (33 loc) · 916 Bytes
/
essay20.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Java Essay Serials 1 - Java Basics - 20, Can one ".java" source file contain multiple classes?
Yes, one '.java' source file can contain multiple classes, but only one is public, and the one must has the same name with the file name.
for example:
Test1.java
public class Test1{
void doTest() {
};
}
class Utility {
String name = "";
Utility() {
}
}
Within the Test1.java file, there are 2 classes Test1 and Utility.
If we set Utility to public, the compiler would complain:
The public type Utility must be defined in its own file.
This rule applies for interface as well.
for example:
interface Worker {
void doSomething(Utility util);
}
public class Test1 implements Worker{
public void doSomething(Utility util) {
}
void doTest() {
};
}
class Utility {
String name = "";
Utility() {
}
}
If we set the interface Worker to public, the same error would be prompted.