Page 1 of 1

[HD Pg 0, Sec. 4.3.4 - relationship-between-java-source-file-name-and-class-name]

Posted: Mon Mar 02, 2020 10:17 am
by BigData
Hi all.

Reading "4.3.4 Relationship between Java source file name and class name":
Q. Does that mean I cannot have multiple classes in a single file?
A. No, you certainly can have multiple classes in a single file. But only one of
them can be public and the name of that public class must be the same as the
name of the file. It is okay even if there is no public class in a file.
I tried to place several public classes in a single java file and compiler did`t produce any errors (I use command line and Java SE v11.0.6). More over, I could successfully run the application. The question is: do I miss something or there is an error in the "OCP Oracle Certified Professional Java SE 11 Programmer I Exam Fundamentals 1Z0-815: Study guide for passing the OCP Java 11 Developer Certification Part 1 Exam 1Z0-815" book?

And on the contrary, when I place one or more private/protected classes in java file, I get compiler error " modifier protected not allowed here". Is this a misprint in the book or I miss something again?

Thank you.

Re: [HD Pg 0, Sec. 4.3.4 - relationship-between-java-source-file-name-and-class-name]

Posted: Mon Mar 02, 2020 11:31 am
by admin
You are doing something wrong. You should see an error if you have more than one top level public class in a file. There is no restriction on inner classes, but that is a different case altogether.

Re: [HD Pg 0, Sec. 4.3.4 - relationship-between-java-source-file-name-and-class-name]

Posted: Mon Mar 02, 2020 12:04 pm
by BigData
admin wrote:
Mon Mar 02, 2020 11:31 am
You are doing something wrong. You should see an error if you have more than one top level public class in a file. There is no restriction on inner classes, but that is a different case altogether.
Yes, that`s my fault, I used default access modifier (i.e. no modifier at all). I changed it to 'public' and compilation error has gone.
But I still can`t compile the java file with one/several private/protected/ methods in it.

Here is the code in Test.java file:

Code: Select all

private class Test {
	private void doSomethingElse() {
		System.out.println("Hello from Test");
	}
}
I compile it with command:

Code: Select all

D:\java\test>javac -d . Test.java
Test.java:7: error: modifier private not allowed here
private class Test {
        ^
1 error
I still can`t understand the nature of the error.

Re: [HD Pg 0, Sec. 4.3.4 - relationship-between-java-source-file-name-and-class-name]

Posted: Mon Mar 02, 2020 12:18 pm
by admin
The error message is quite clear. You can't have private modifier for a top level class. This is also mentioned in section 8.4.4 "Applying access modifiers to types of the book:
A top level class (i.e. a class that is not defined inside another reference type) can only have two types of access - public and default.

Re: [HD Pg 0, Sec. 4.3.4 - relationship-between-java-source-file-name-and-class-name]

Posted: Mon Mar 02, 2020 12:46 pm
by BigData
I haven`t reached that section yet, just in the very beginning. But this conflicts with book`s statement that "It is okay even if there is no public class in a file" from the quote in my initial post.

Re: [HD Pg 0, Sec. 4.3.4 - relationship-between-java-source-file-name-and-class-name]

Posted: Mon Mar 02, 2020 10:22 pm
by admin
Not sure which two statements you think are in conflict. I don't see any conflict.