简体   繁体   中英

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 . 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 . 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). When you execute a program, static members - that "belong" to a class, like field1 in your question - are allocated once for your whole program. The other "normal" class members - like field2 in your question - are allocated for each new instance you create.

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. Got 1000 instances of ExampleClass? There's still only one.

Those sizes can be smaller, depends on the VM configuration.

So how would java know that an instance of ExampleClass has a method named instanceMethod ? By following the pointer to the data structure describing ExampleClass itself and noticing that it has that. 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.

static variables and methods do not increase the size of an Object instance. These fields and methods are actually added to the Class object.

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).

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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