This article involvesJava Accessability and some content of internal classes show readers a special phenomenon of internal classes. Through this example, developers can learn some details that they may not have noticed before, it may help developers Better Understand java's accessible features, internal classes, and virtual machines. By analyzing these details, developers may think about and analyze problems and use appropriate tools.
Java access modifier includes Default-Access, public, private, and protected. Not all situations can use all four types of access modifiers. In some cases, using some access modifiers is meaningless. For example, if a class is not an internal class, you cannot use private as the access modifier for this class. Compile the following code:
Private class test
{...}
The jdk compiler will provide the "modifier private not allowed here" error message. Because the private feature can only be used by the class that defines it. If the above Code is compiled, the test class will not be available in any circumstances, so it will not make any sense.
If a class does not define any constructor, the compiler will generate a default constructor. The access modifier of this constructor is the same as the access modifier of the class. For example:
Class test will generate the test () constructor public class test will generate the public test () constructor.
When using internal classes, the above features will make the compiler show a special phenomenon. It should be noted that the following example only applies to jdk compilers in Windows, and the author has not tried to use other compilers. However, since the java compiler generates code in the intermediate form of a class file, the following discussion should be applicable to any compiler that complies with java standards.
Compile the following code:
Public class Wrapper
{
Private class InnerClass
{}
Private void testInnerClass ()
{
InnerClass inner = new InnerClass ();
}
Public static void main (String [] args)
{
Wrapper wrapper = new Wrapper ();
Wrapper. testInnerClass ();
}
}
Three class files will be generated: Wrapper, Wrapper $ InnerClass, and Wrapper $1.
Readers of the first two files will understand the internal class, but what is the role of the third class Wrapper $1?
Using the reflection mechanism of java or the javap anti-assembler, you will find that the Wrapper $1 class does not have any member variables and methods, while the Wrapper $ InnerClass has a private Wrapper $ InnerClass () in addition to the constructor, there is also a Wrapper $ InnerClass (Wrapper $1) constructor. Using javap, you will find Wrapper $ InnerClass (Wrapper $1) the Wrapper $1 type parameter is not used, but private Wrapper $ InnerClass () is called directly (). If you think about the process of creating a new class instance, you probably understand the cause of the above phenomenon: when the program tries to create a Wrapper $ InnerClass class instance, however, the default constructor cannot be used because Wrapper $ InnerClass () is private and cannot be used externally. Therefore, the compiler has to generate an accessible constructor. Because only the Wrapper class's private void testInnerClass () method uses new InnerClass (), the compiler only needs) the Default-Access modifier is generated for the new constructor. In addition, in order to be different from the existing default constructor, a Wrapper $1 parameter is added. To this end, the compiler also generates a Wrapper $1 class.
For the sake of simplicity, (perhaps) a clearer view of the working principle of the class code generated by the compiler, you can use the java anti-compiler to look at the java source program generated after the class is decompiled, the following is the Wrapper class code generated after the author decompiled it using Jad:
// Decompiled by Jad v1.5.7d. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports (3)
// Source File Name: Wrapper. java
Public class Wrapper
{
Private class InnerClass
{
Private InnerClass ()
{
}
InnerClass (_ cls1 _ pcls1)
{
This ();
}
}
Public Wrapper ()
{
}
Private void testInnerClass ()
{
InnerClass innerclass = new InnerClass (null );
}
Public static void main (String args [])
{
Wrapper wrapper = new Wrapper ();
Wrapper. testInnerClass ();
}
// Unreferenced inner classes:
/* Anonymous class */
Class _ cls1
{
}
}
Obviously, the Wrapper $1 class does not have any practical effect. So why does the compiler need to generate the Wrapper $1 class instead of using any basic type (such as byte) as a placeholder? I think, probably because using Wrapper $1 can use less memory, because an empty class does not occupy any memory (the Wrapper $1 class does not have any member variables, it does not need any pointer variable pointing to it. In fact, even if you delete Wrapper $1. the class file does not affect the program running, and the jvm will not give any error messages .), Any parameter that can have actual values requires some memory to store it. So Will java compilers not be optimized? The problem is that the java compiler only produces class code. At the class code level, such optimization cannot be expressed to virtual machines. The Java Virtual Machine is probably not suitable for this optimization feature, so sun adopts the current solution. It should be noted that this is just my guess. As I have not read all jvm documents in detail, I cannot be sure.