Is "Conditional Classes" a Java tutorial wrong with respect to private classes?

I read the Java documentation here , and here is what I found:

You can also add modifiers, such as public or private, at the very beginning so that you can see that the initial line of the class declaration can become quite complex. The public and private modifiers, which determine that other classes can access MyClass , are discussed later in this lesson.

It indicates that I can create a class with private or public . However, when I try to use the private modifier, I get an error that it is an illegal modifier for this class: only public , abstract and final are allowed.

I understand that the private modifier is not useful, but why does this tutorial, which is located on the official Java site, indicate that I can create a class using it? Did I miss something?

+4
source share
10 answers

Top-level classes cannot be private . However, nested classes can be private .

See JLS

The public access modifier (§6.6) applies only to top-level classes (§7.6) and member classes (§8.5), not local classes (§14.3) or anonymous classes (§15.9.5).

Access modifiers, protected and private (§6.6), apply only to member classes in a class declaration with direct inclusion or enumeration (§8.5).

+15
source

You cannot have a top-level private class. You can have an internal or nested private class.

Obviously, the top-level private class would be somewhat useless, since you would not be allowed to access it from anywhere.

So this is allowed

 public class MyClass { private class MyInnerClass { } } 
+3
source

It's not a mistake; look carefully at the wording:

You can also add modifiers as public or private at the very beginning -......

For regular classes, you can have public and standard (private, non-word-changing) visibility. Nested can be closed or projected.

+1
source

"" Invalid modifier for class, only public, abstract, and final are allowed. "" "

upper class cannot be closed

when you try to name a file using the name of a private class.

you cannot use the same name for a file and a private class.

class name and file name are the same if and only the class is not private ..

if the class is public, then the file name and class name must be the same.

If there are many public classes, the file name must be public. .

+1
source

You may have a private inner class to be applicable.

0
source

You can use them for Inner classes. I suspect you were trying to use it for a top level class. Here is an example of using private classes

0
source

The primary class in the .java file does not have to be private (for it it would be a useless file to be private, because how it will be used), however you can have inner classes that are used by the main class in the file, they can be private.

for instance

 public class OuterClass{ //constructors and methods of OuterClass private class InnerClass{ //constructors and methods of InnerClass } } 
0
source

Private classes are allowed, but only as inner or nested classes.

0
source

private allowed for nested classes:

 public class A { private B b; private class B {} // Only class A can use this class } 
0
source

You can always have an inner class as private.

Below is the full version.

 public class Foo { private class Bar { } } 
0
source

Source: https://habr.com/ru/post/1489153/


All Articles