Private Access Applied to a Class

The private access modifier cannot be applied to a standard outer class. In Java, sometimes it matters just as much what you cannot do versus what you can do. An exam test question may have some complicated logic located inside of a class marked private. If you notice the private class in the declaration, you won't waste your time figuring things out and you can just answer something like fails to compile. Okay, what happens when we try to apply private access to a class? Let's do it.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md one
C:\Java>cd one
C:\Java\one>Notepad Tester.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package one;

private class Tester { }

Now switch back to the command prompt (CMD) and type in the following commands.


C:\Java\one>javac Tester.java
Tester.java:3: error: modifier private not allowed here
private class Tester { }
          ^


Final thoughts

The only modifier that can be applied to a standard outer class is public. A standard outer class without a modifier is implicitly assigned default (package-private) access.


Tutorials