简体   繁体   English

在Java中,在使用Java创建对象期间,静态方法在哪里?

[英]In Java where do the static methods go during the creation of an object in java?

If there is a class with some methods, whenever we create a new object then that object gets created on the heap with the fields and methods. 如果存在带有某些方法的类,则每当我们创建一个新对象时,该对象便会在具有字段和方法的堆上创建。 If we have a class with static and normal methods and we create an object of this class, the instance variables and normal methods still exist but how are the static methods created ? 如果我们有一个带有静态和普通方法的类,并且创建了此类的对象,则实例变量和普通方法仍然存在,但是如何创建静态方法呢?

All methods, both static and non-static, are part of the class , and classes are stored in non-heap memory. 所有方法(静态方法和非静态方法)都是class的一部分,并且类存储在非堆内存中。

All that resides in the heap is a structure containing: 驻留在堆中的只是一个包含以下内容的结构:

  • a pointer to the class of which this object is an instance 指向此对象是其实例的类的指针
  • The current values for each non-static field (references for object fields, values for primitive fields) 每个非静态字段的当前值(对象字段的引用,原始字段的值)

So for example: 因此,例如:

 public class Person {
     private String name;
     private int age;

     public String getName() {
         return name;
     }

     // ... etc.
 }

One structure in non-heap memory contains: 非堆内存中的一种结构包含:

  • Metadata about the class eg its name Person , a pointer to its superclass etc. 有关类的元数据,例如其名称Person ,指向其超类的指针等。
  • The bytecode for getName() and any other methods getName()和任何其他方法的字节码
  • JIT optimised versions of the bytecode JIT优化的字节码版本

For each instance of Person , we get in the heap: 对于Person每个实例 ,我们进入堆:

  • A reference to the class entry 对类条目的引用
  • An int value for age age的整数值
  • A reference to a String object name String对象name引用

So, at least conceptually, you could see the runtime handling a call to aPerson.getName() as: 因此,至少在概念上,您可以看到运行时aPerson.getName()的调用处理为:

  • Look at the object's record in heap 查看堆中的对象记录
  • Follow the reference to its class 遵循其类的参考
  • In the class, find the method getName() 在该类中,找到方法getName()
  • Run the method, using the object's record in the heap for fields 使用对象在堆中的记录作为字段运行方法

This means that if you have 1000 instances of Person , you still only have one copy of the code from the class, which is perfect because it does not change from one instance to the next. 这意味着,如果您有1000个Person实例,则仍然只有该类中代码的一个副本,这很完美,因为它不会从一个实例更改为另一个实例。

If Doctor , Nurse , Patient are all subclasses of Person , and if none of them override getName() , then the code for getName() still only exists once in memory. 如果DoctorNursePatient是所有子类Person ,如果没有他们的覆盖getName()然后为代码getName()仍然只存在于内存中一次。 (Conceptually) when you call aNurse.getName() the runtime will look in Nurse for getName() , if found, run that, otherwise look in the superclass Person , find it, and run that. (从概念上讲)当您调用aNurse.getName() ,运行时将在Nurse查找getName() ,如果找到,请运行它,否则请在超类Person中查找,找到并运行。


I've said "conceptually" a couple of times. 我已经“概念上”说了几次。 In reality some of what I've described as happening dynamically may happen statically at compile time. 实际上,我所描述的某些动态发生可能在编译时静态发生。 This doesn't really matter as far as understanding the effect on memory and performance. 就了解对内存和性能的影响而言,这并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM