G
G
go2goj2019-12-19 14:30:53
Java
go2goj, 2019-12-19 14:30:53

When a class is initialized, are its instace members loaded into memory?

When a class is initialized (for example, when we access its static fields for the first time, without creating an object), are its instace members loaded into memory? Or is there only static members in RAM?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vamp, 2019-12-20
@go2goj

They may load or they may not. JVMS does not specify whether all, some, or none of the types referenced by the class being loaded should be loaded. So this behavior is at the discretion of the JVM developer.
HotSpot, for example, implements lazy class loading. That is, it does not load classes unless it is strictly necessary. It may not even load all static members during class static initialization.
Example:

import java.time.LocalDateTime;
import java.util.concurrent.CountDownLatch;

public class A {

    private static LocalDateTime ldt;

    private static CountDownLatch cdl;

    static {
        ldt = LocalDateTime.now();
        cdl = null;
    }

    public static void main(String[] args) {
        System.out.println("123");
    }

}

Running this code with the "-verbose:class" JVM argument will list all loaded classes. Run example on openjdk 12:
...
[0,100s][info][class,load] java.time.temporal.TemporalAccessor source: shared objects file
[0,100s][info][class,load] java.time.temporal.Temporal source: shared objects file
[0,100s][info][class,load] java.time.temporal.TemporalAdjuster source: shared objects file
[0,101s][info][class,load] java.time.chrono.ChronoLocalDateTime source: shared objects file
[0,101s][info][class,load] java.time.LocalDateTime source: shared objects file
...

It can be seen that LocalDateTime and all interfaces implemented by it have loaded, but CountDownLatch is not in the list, despite the fact that there is a static variable with this type.
The same happens with instance members - the classes they refer to will only be loaded as needed (used). It is quite normal for objects of the same class to be actively used in a program, but not all types used by it are loaded. Moreover, this applies not only to members of a class or object, but also to local variables and even just code:
import java.math.BigDecimal;
import java.time.LocalDateTime;

public class A {

    public static void main(String[] args) {
        if ("world".equals(System.getenv("HELLO"))) {
            System.out.println(LocalDateTime.now());
        } else {
            System.out.println(BigDecimal.TEN);
        }
    }

}

This example will load the BigDecimal class, but not the LocalDateTime. If you invert the condition or run the code with the HELLO=world environment variable set, then LocalDateTime will appear in the list of classes, but BigDecimal will be absent.

D
Dmitry Roo, 2019-12-19
@xez

public class Person {
    static FullName staticFullName;
    FullName fullName;
    int age;
}

public class FullName {
    String name;
    String surname;
}

public class Test {

    public static void main(String[] args) {
        Person person = new Person();

        System.out.println("This is age " + person.age);
        System.out.println("This is fullName " + person.fullName);
        System.out.println("This is static fullName " + Person.staticFullName);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question