Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly do static fields work internally? [duplicate]

Say you have a class,

class Foo {     public static bar; } 

When you say:

new Foo(); 

I can imagine that in memory, a space is reserved for this object.

...and when you say again:

new Foo();  

...well now you have another space available for the object.

However, where exactly does the static field live?

What I am really trying to learn is:

How do the references to the objects reference the same field of the objects they reference?

like image 299
Koray Tugay Avatar asked Feb 08 '13 21:02

Koray Tugay


People also ask

How many copies of a static field are there?

There will always be only one copy of static field belonging to it. The value of this static field is shared across all objects of either the same class. From the memory perspective, static variables are stored in the heap memory.

Why static is always single copy?

Memory allocation for a static variable happens only once in the class area when the class is loaded in the memory. It is also known as a class variable. It is common to all the objects of the class. In this, a single copy of a static variable is created and shared among all the objects of the class.

How do static fields and methods work?

The static keyword in Java can be applied to both fields and methods of a class. A field or method of a class that is static can be accessed without an instance of the class. Think of a situation where you want to store information that is the same for everyone and functions that behave the same way for everyone.

Why should we avoid using static for everything?

Static methods aren't part of the object, so they don't have access to anything that belongs to the object. Instead, they belong to the underlying class of the object. Hence, static methods can only access static variables - and we've already declared them taboo.


1 Answers

While the exact details of the type system are implementation dependent, let me go into some more detail than just stating that it depends and you should not care. I'll describe how it approximately works in Microsoft's implementation (.NET) according to the book CLR via C# by Jeffrey Richter and the article See How the CLR Creates Runtime Objects by Hanu Kommalapati et al. (original MSDN May 2005 issue).


Say you have a class:

class Foo {     // Instance fields     string myBar = "Foobar";     int myNum;      // Static fields     static string bar = "Foobar";     static int num; }  Foo myFoo = new Foo(); Type typeOfFoo = typeof(Foo); 

Where do the instance fields live?

Whenever you say new Foo(), space is allocated and initialized for the object instance, and the constructor is called. This instance is shown as instance of Foo in the image below. Such as instance contains only the instance fields of the class (in this case myBar and myNum), and for objects allocated on the heap two extra fields used by the runtime (Sync block index and Type handle). The type handle is a pointer to a Type object that describes the type of the instance, in this case type of Foo.

When you say new Foo() again, new space is allocated which will again contain space for the instance fields of the type. As you can see, instance fields are associated with object instances.

The runtime puts each instance field at a fixed offset from the start of the object's data. For example, myBar might live at offset +4. The address of the instance field is simply the address of the object plus the offset of the field.

Where do the static fields live?

Static fields in C# and Java are not associated with any object instance, but with a type. Classes, structs and enums are examples of types. Only once (per type) is some space allocated to hold the values of the static fields. It would make sense to allocate space for the static fields in the Type structure that describes the type, since there is also only one Type object per type. This is the approach taken by C# and Java.

The Type object1 is created when the type is loaded by the runtime. This structure contains all sorts of information needed for the runtime to be able to allocate new instances, call methods and perform casting, among other things. It also contains the space for the static fields, in this case bar and num.

The runtime has put each static field at some offset from the start of the type's data. This is different for each type. For example, bar might live at offset +64. The address of the static field is the address of the Type object plus the offset of the field. The type is statically known.

Displays some object structures, and their relationships.

1) In Microsoft .NET multiple different structures describe a type, such as the MethodTable and the EEClass structures.

like image 68
Daniel A.A. Pelsmaeker Avatar answered Oct 14 '22 02:10

Daniel A.A. Pelsmaeker