Home  >  Q&A  >  body text

java - new + 类名,一定需要申明一个对象吗?

public class CodeBlock02
{
    {
      System.out.println("第一代码块");    
    }
    
    public CodeBlock02()
    {
        System.out.println("构造方法");
        }
        
        {
          System.out.println("第二构造块");
      }
   public static void main(String[] args) 
    {
          new CodeBlock02();
          new CodeBlock02();
          new CodeBlock02();
           
    }
}    

在这里, new CodeBlock02(); 或者换成 CodeBlock02 code = new CodeBlock02();
他们是一样的吗!

巴扎黑巴扎黑2743 days ago587

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:55:33

    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 Exit, 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 of the stack.

    • 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 memory managed by JVM. It can be understood that direct memory is machine memory other than JVM. For example, if you have 4G of memory and 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 what object references are in Java and what is their relationship with the instantiation process of objects.

    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 question’s example:

    CodeBlock02 code = new CodeBlock02();

    code is a reference stored in the local variable table, which points to the object instance data in the heap. This object instance data is obtained through new CodeBlock02().

    Be more specific:

    1. 你写的 CodeBlock02.java 文件存放了 CodeBlock02 类的定义,当 jvm 的类加载器加载这个java文件的时候,将其中的类型定义语句存放在了 jvm 的方法区中。
    
    2. 但是这个时候并没有在堆中生成这个对象的实例,也就是说,这个时候因为没有对象,你并不能调用 CodeBlock02 类的非静态方法。
    
    3. 什么时候获取的对象呢?就是在用 new 关键字执行了本类的构造方法以后 new CodeBlock02() 从这时候开始通过 new 关键字和类的构造器, jvm 在虚拟机的堆区创建了一个 CodeBlock02 类的实例,并返回这个实例的引用,同时你也可以通过这个引用调用它的非静态方法了。
    

    To sum up, code is the "remote control" you use to receive the instance produced by new. It points to the specific location of this object in the heap area.

    reply
    0
  • PHPz

    PHPz2017-04-18 10:55:33

    You need to understand java’s quotes

    CodeBlock02 code = new CodeBlock02();

    The one on the left is called a variable of type CodeBlock02.

    The one on the right is called an object of type CodeBlock02.

    You can also let this variable point to two different objects of the same type in turn.

    CodeBlock02 code;
    CodeBlock02 code1 = new CodeBlock02();
    CodeBlock02 code2 = new CodeBlock02();
    code = code1;
    //code.doSomething(); 相当于 code1.doSomething();
    code = code2;
    //code.doSomething(); 相当于 code2.doSomething();

    You can even make variables of this type point to objects of subclasses of this type:

    MyClass m = new SubMyClass(); //SubMyClass 继承于 MyClass

    You can also call methods directly on the object from new like this:

    new CodeBlock02().doSomething();

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:55:33

    Both are declaration objects. What the poster is asking about should be assignment

    If you don’t continue to operate on this value later, it will be the same whether you assign it or not

    new CodeBlock02() // If no value is assigned after declaration, there is no way to continue operating on this object
    CodeBlock02 code = new CodeBlock02(); // Assign the declared object to a variable, and subsequent operations can be performed

    reply
    0
  • PHPz

    PHPz2017-04-18 10:55:33

    The one on the left is the reference variable of the object, and the one on the right is the object actually allocated in the memory.

    reply
    0
  • Cancelreply