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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
BigData
Posts: 3
Joined: Tue Feb 25, 2020 5:57 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

BigData
Posts: 3
Joined: Tue Feb 25, 2020 5:57 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

BigData
Posts: 3
Joined: Tue Feb 25, 2020 5:57 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Not sure which two statements you think are in conflict. I don't see any conflict.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 10 guests