Inner classes.

As already mentioned, nested classes have access to all members of the outer class, including even private fields and methods. The question may arise, how do nested classes get additional access privileges, because they are converted to ordinary ones, and the virtual machine does not know anything about them at all?

We will study all this in one example, where we will try to access a private class field using dummy classes. As mentioned, inner classes have access to all members of the outer class. The virtual machine does not know anything about inner classes, since the compiler converts all inner classes to top-level classes and names them according to the rules that we have already discussed several times.

The idea is to create a class that primitively contains a password in a private field. And it will have only one method that checks the string entered as a parameter for whether or not it matches the password stored in a private field. But this method does not return the value of this field, but only displays a message whether the password is correct or not.

Then, in another project, we will try to create an inner class that will try to access this private field.

[S0001%255B2%255D.png]
S0002

So! We have a PassCheck class that has a private PASSWORD field containing the password. It also has a method that allows you to check the value passed to it.