简体   繁体   English

静态方法和字段是否在定义它们的类的实例中占用内存?

[英]Do Static Methods and Fields take up memory in an instance of the class they are defined in?

For example if I were to create the following class:例如,如果我要创建以下类:

public Class ExampleClass {
  
  private static String field1;
  private String field2;

  public ExampleClass(String field2) {
    this.field2 = field2;
  }

  public static staticMethodExample(String field1) {
    this.field1 = field1;
  }
}

If I were to create an instance of ExampleClass .如果我要创建ExampleClass的实例。 Would that instance contain the code for the static method and/or field I created?该实例是否包含我创建的静态方法和/或字段的代码?

I have an object that will represent some data from a row in my database.我有一个对象将代表我数据库中一行的一些数据。 I would like to create the a list of these objects from every row in the database.我想从数据库中的每一行创建这些对象的列表。 The database has thousands of rows.数据库有数千行。 The static methods I am creating will format the values from the database before putting them into the objects constructor.我正在创建的静态方法将在将数据库中的值放入对象构造函数之前对其进行格式化。

I don't want to bloat the code by having the method stored in every instance of the object.我不想通过将方法存储在对象的每个实例中来膨胀代码。 So if static methods do take up space in every instance of an object, then I would much rather create a separate class of of a name like ExampleClassBuilder .因此,如果静态方法确实在对象的每个实例中都占用了空间,那么我宁愿创建一个名称为ExampleClassBuilder的单独类。 And put the formatting static methods in there.并将格式化静态方法放在那里。

No, static methods and fields do not take space in an instance of the class.不,静态方法和字段在类的实例中不占用空间。

You are making a number of confusions here.你在这里制造了一些混乱。 When you compile your program, the code of each of your method (static or not) is "stored" in your compiled program (in the case of Java, in .class files).编译程序时,每个方法(静态或非静态)的代码都“存储”在已编译的程序中(在 Java 的情况下,在.class文件中)。 When you execute a program, static members - that "belong" to a class, like field1 in your question - are allocated once for your whole program.当您执行一个程序时,静态成员- “属于”一个类,如您问题中的field1 - 为您的整个程序分配一次。 The other "normal" class members - like field2 in your question - are allocated for each new instance you create.其他“普通”类成员 - 如您问题中的field2 - 为您创建的每个新实例分配。

When you create a new instance of an object, the code of its various methods is not "allocated", as it already exists in compiled form in your program.当你创建一个对象的新实例时,它的各种方法的代码没有被“分配”,因为它已经以编译形式存在于你的程序中。

The data on the heap for an instance of this class would look roughly like*:此类实例的堆上的数据大致如下*:

size (bytes) | description
--------------------------
8            | A pointer to the singleton object representing ExampleClass
8            | A pointer to the value stored in field2

That's it.就是这样。 The static fields aren't stored.不存储静态字段。 And none of the methods are, not even instance methods!没有一个方法是,甚至不是实例方法! It's just the non-static fields.这只是非静态字段。 That pointer to the singleton?那个指向单例的指针? There is only one data structure representing what ExampleClass is for the entire VM.只有一种数据结构代表了整个 VM 的 ExampleClass。 Got 1000 instances of ExampleClass?有 1000 个 ExampleClass 实例? There's still only one.仍然只有一个。

Those sizes can be smaller, depends on the VM configuration.这些大小可以更小,具体取决于 VM 配置。

So how would java know that an instance of ExampleClass has a method named instanceMethod ?那么 java 怎么知道 ExampleClass 的实例有一个名为instanceMethod的方法呢? By following the pointer to the data structure describing ExampleClass itself and noticing that it has that.通过跟踪指向描述 ExampleClass 本身的数据结构的指针,并注意到它具有该结构。 This:这个:

class Foo {
    public void hello() {}
}

is basically syntax sugar for:基本上是语法糖:

class Foo {
    public static void hello(Foo receiver) {}
}

But note that instance methods do dynamic dispatch and static methods don't.但请注意,实例方法执行动态分派,而静态方法则不然。 That should explain why methods (even instance methods) aren't taking up any 'memory' per instance.这应该可以解释为什么方法(甚至实例方法)不占用每个实例的任何“内存”。 Only once for the entire class.整个班级只有一次。 The same trick cannot be applied to instance fields.同样的技巧不能应用于实例字段。

*) This is highly oversimplified and not specified by either the JLS or the JVMS, but it reflects what just about every popular VM out there actually does. *) 这过于简单化了,JLS 或 JVMS 都没有指定,但它反映了几乎所有流行的 VM 的实际作用。

static variables and methods do not increase the size of an Object instance.静态变量和方法不会增加 Object 实例的大小。 These fields and methods are actually added to the Class object.这些字段和方法实际上是添加到 Class 对象中的。

No: When we create a static variable or method it is stored in the special area on heap: Metaspace (for Java 8 and Permgen for older versions).否:当我们创建静态变量或方法时,它存储在堆上的特殊区域:元空间(对于 Java 8 和Permgen对于旧版本)。

Whenever a class is loaded static variables as well as methods are loaded into memory allowing any instance(which is discouraged ) or direct use of them.每当一个类被加载static变量以及方法被加载到内存中时,允许任何实例(不鼓励)或直接使用它们。 So, any object can change the value of the variable, but they can also be manipulated without creating an instance of the class.因此,任何对象都可以更改变量的值,但也可以在不创建类的实例的情况下对其进行操作。 Check here for more https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html在此处查看更多https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

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

相关问题 类实例中的方法是否在内存中占有一席之地? - Do methods in class instances take a place in memory? null SQLite数据字段是否会占用额外的内存? - Do null SQLite Data fields take up extra memory? 声纳:实例方法不应写入“静态”字段 - Sonar: Instance methods should not write to "static" fields static object 经历的所有值是否在 memory 中占用空间,而 ZA2F2ED4F8EBC0661C21A291Z 已加载? - Does all values which a static object went through take up space in memory while class is loaded? Singleton类仅具有静态字段和方法,但在内存中具有许多实例 - Singleton class only has static fields and methods but has many instances in the memory 静态方法和实例方法的内部内存表示形式之间的区别 - Difference between internal memory representation of static methods and instance methods 如何抑制Java类实例上的静态方法? - How to suppress static methods on class instance on Java? 类实例和静态方法之间的关系 - Relation between class instance and static methods 为什么在创建 VBO 时静态方法会占用内存? - Why do static methods run up memory when creating VBO's? 返回对具有静态方法和静态字段的类的引用,而无需实例化 - Return a Reference to a Class with Static Methods and Static Fields Without Instantiation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM