Home  >  Q&A  >  body text

java在类的内部创建本类的对象是怎么做到的?不理解啊?

ringa_leeringa_lee2743 days ago505

reply all(8)I'll reply

  • 黄舟

    黄舟2017-04-18 10:53:14

    First, let’s clarify a few concepts. Java code runs in jvm, and the memory area of ​​jvm is divided into several modules:

    • Program Counter (Program Counter Register): The program counter is a relatively small memory area used to indicate which line of bytecode executed by the current thread has been executed. It can be understood as the line number indicator of the current thread. . When the bytecode interpreter is working, it will fetch a statement instruction by changing the value of this counter.

    • Virtual Machine Stack (JVM Stack):When each method of a thread is executed, a stack frame (Statck Frame) will be created. The stack frame stores local variable tables, operation stations, dynamic links, methods Exits, etc., when the method is called, the stack frame is pushed into the JVM stack, and when the method execution is completed, the stack frame is popped out.

    • Native Method Stack: The native method stack is the same as the virtual machine stack in terms of function, operating mechanism, exception types, etc. The only difference is that the virtual machine stack executes Java methods, while native methods The stack is used to execute native methods. In many virtual machines (such as Sun's JDK default HotSpot virtual machine), the native method stack and the virtual machine stack are used together.

    • Heap: The heap area is the most important area to understand the Java GC mechanism, bar none. The heap area is the largest piece of memory managed by the JVM. The heap area is also the main memory area managed by the Java GC mechanism. The heap area is shared by all threads and is created when the virtual machine starts. The heap area exists to store object instances. In principle, all objects are allocated memory on the heap area (but in modern technology, this is not so absolute, and some objects are allocated directly on the stack).

    • Method Area: (also known as the permanent generation), the method area is an area shared by each thread, used to store class information that has been loaded by the virtual machine (that is, the information that needs to be loaded when loading a class, Including version, field, method, interface and other information), final constants, static variables, code compiled by the compiler on-the-fly, etc.

    • Direct Memory: Direct memory is not the memory managed by the JVM. It can be understood that direct memory is the machine memory other than the JVM. For example, if you have 4G of memory and the JVM occupies 1G, then the rest 3G is direct memory. There is a memory allocation method based on channel and buffer in JDK. The native function library implemented in C language is allocated in direct memory and referenced by DirectByteBuffer stored in the JVM heap. Since direct memory is limited by the memory of this machine, an OutOfMemoryError exception may also occur.

    After understanding these basic concepts, let’s look at the areas where the questioner has doubts. In fact, what the questioner is wondering about is how object references are implemented in Java. Why can you define your own reference while defining a class? At the same time, if you instantiate this reference, won't it lead to an infinite circular reference?

    Don’t worry, let’s first analyze how a reference is implemented in Java:

    A Java reference access involves three memory areas: JVM stack, heap, and method area.

    Taking the simplest local variable reference: Object obj = new Object() as an example:

    • Object obj represents a local reference, which is stored in the local variable table of the JVM stack and represents a reference type data;

    • new Object() is stored in the heap as instance object data;

    • The address of the type information of the Object class (interface, method, field, object type, etc.) is also recorded in the heap, and the data executed by these addresses is stored in the method area;

    There are many specific implementation methods, handle is one of them, and the relationship is as shown in the figure.

    You should understand it when you see this. The information of the class itself, class instance data, and reference information pointing to the object are placed in the method area, stack area, and heap area of ​​java respectively.

    In the example of the subject, the java loading order is like this:

    1. jvm first loads the class definition in the method area (but this class is not instantiated at this time)

    2. Because public static final Direction FRONT = new Direction(); is a static variable, this variable will also be loaded into the method area when the jvm reads the method area definition for the first time.

    3. At the same time, this also means that while loading this variable, an instance of this class is also instantiated in the heap area.

    Pay attention to the key point here, because the FRONT variable is a static variable, and the loaded class definition will only be loaded once, so this static variable can only be loaded once. It does not cause stack overflow due to repeated instantiation of circular references like non-static variables.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:53:14

    Recommend you to read R’s answer

    Which comes first, Class or Object?
    https://www.zhihu.com/questio...

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:53:14

    Tell me your understanding, why can’t you create your own objects in a class?
    After adding static, these variables become attributes of the class and will only be created once.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:53:14

    If you can’t create yourself, then other classes can’t even create it. In this case, how to instantiate this class...

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:53:14

    Design Pattern: Singleton Pattern

    reply
    0
  • PHPz

    PHPz2017-04-18 10:53:14

    The essence is a lack of understanding of object-oriented programming in Java. Take a look at the 23 design patterns and you may understand

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:53:14

    1. Constructor is also a method.

    2. Methods with private access rights are private and visible only to this class.

    So, this class can call the constructor with private access rights to instantiate an object.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:53:14

    Reasons for using inner classes: Each inner class can independently inherit from an (interface) implementation, so whether the outer class has inherited an (interface) implementation, it has no impact on the inner class. In fact, inner classes effectively implement "multiple inheritance", that is, inner classes allow inheritance of multiple non-interface types.

    We know that inner classes automatically have access to all members of outer classes, so how is this done? When an outer class object creates an inner class object, the inner class object must secretly capture a reference to that outer class object. Then, when you access a member of the outer class, you use that reference to select the member of the outer class. Of course these details are handled by the compiler, and the inner class here is non-static.
    If a class cannot create its own class object, then what is the use of your class? Ah, hahahaha, just kidding

    reply
    0
  • Cancelreply