11

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.

  • Static data and static methods do not take up memory in individual instances. – khelwood Oct 30 '20 at 15:49
  • Welcome to SO @inTHEsiders. Consider the following statement "**a static class can not be instantiated**". Can you answer that question on your own then? Where [static class fits this description](https://stackoverflow.com/questions/1844355/java-static-class). – dbl Oct 30 '20 at 15:56

4 Answers4

10

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.

Patrick
  • 1,458
  • 13
  • 27
  • 1
    Thanks @Patrick. That's what I thought, but I wasn't entirely sure. – Matthew Cole Anderson Oct 30 '20 at 16:27
  • The common argument that inadvertently becomes very misleading on this topic is the one that talks about class length (Rule 30) . I particularly find that the length of 200 lines (which is the most recommended) is just too short when taking into account static classes and methods that are intrinsically linked/relevant to the class they inhabit. If a pojo is allocated and used on 2 different places on an app, it can easily surpass 500 lines just by defining view functions alone that are exclusive to the pojo on the other hand the rule itself defines it at 900. – Delark Nov 10 '21 at 21:10
5

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I thought this was the case for instance methods. It would be a poor design if every instance method were copied rather than just use tables to locate the arguments and pass on to the methods when invoked. – WJS Oct 30 '20 at 16:22
  • this! very much this! it is exactly the reason of why an instance method does `aload_0` first... a nitpick is that one of those pointers is (in the majority of cases) 4 bytes. – Eugene Oct 30 '20 at 17:12
2

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

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

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

Aman
  • 1,627
  • 13
  • 19