When you initialize the class is loaded into memory it instace member?

Class initialization (for example, when we appealed to his static-fields, no object is created) are loaded in the memory of his instace member? Or memory is only static members?
April 3rd 20 at 18:41
2 answers
April 3rd 20 at 18:43
Solution
Can boot, but can not. JVMS does not specify whether to load all, some or none of any types referenced from the loaded class. So this behavior is at the discretion of the developer JVM.

HotSpot, for example, implements lazy loading of classes. That is, does not load the classes unless it is strictly necessary. He may not even all members of the static load during the static initialization of the class.

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");
}

}

When you run this code with the JVM argument "-verbose:class" displays a list of all loaded classes. An example of running on openjdk 12:

...
[0,100 s][info][class load] java.time.temporal.TemporalAccessor source: shared objects file
[0,100 s][info][class load] java.time.temporal.Temporal source: shared objects file
[0,100 s][info][class load] java.time.temporal.TemporalAdjuster source: shared objects file
[0,101 s][info][class load] java.time.chrono.ChronoLocalDateTime source: shared objects file
[0,101 s][info][class load] java.time.LocalDateTime source: shared objects file
...

It is seen that booted LocalDateTime and all of them to be implemented the interfaces, but CountDownLatch is not in the list, despite the fact that there is a static variable with this type.

This also happens with instance members of the classes to which they refer, will be loaded only as needed (use). It is a normal situation, when objects of one class are actively used in the program, but not all of the types 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 LocalDateTime. If to invert the condition or to execute code with the set environment variable HELLO=world, in the list of classes will appear LocalDateTime, but BigDecimal is not available.
For the beginning let's define that class initialization occurs if:
–there is a call to static members of this class or its subclass
–created based on this class object
(... for the first time since the start of the program).
(... not somewhere in unreachable code, in fact).
(Yet I don't get involved evidence that the determination is not correct, so I will come out of it).

Therefore:
1. LocalDateTime loaded because its basis was created the object.
CountDownLatch is not loaded, because it is not supposed to determine.
BigDecimal is loaded because there was an appeal to its members, and the object created.
LocalDateTime is not loaded, because it is not supposed to determine.
(That is, your examples are not something new to me, not saying all, by definition).

2. The question was whether the initialization of instance members in the class initialization, if the object is not created at all? I checked using "-verbose:class", is not included: if no object – what is after the = in the instance-fields is not performed.

3. But the questions about the methods. Please answer:
3.1. Initialize the class (without creating an object based on it). After that, the callee is a static method of this class. Where this time is taken to execute the method body from .class file on your hard drive or from RAM?

3.2 Initialize the class (without creating an object based on it). After that, create an object based on this class. Where exactly in this moment the information is taken to initialize instance fields: out .class file on your hard drive or from RAM? - Aiyana commented on April 3rd 20 at 18:46
3.1, 3.2. Is taken from RAM. There is no such that have downloaded only part of a class, and then as needed pogruzhayutsya the rest of the class. A class is either loaded into memory and creates an object of class class for him, or not start at all with generating appropriate exception - ClassNotFoundException, NoClassDefFoundError, VerifyError, LinkageError etc. to Create objects and to call methods (static and non-static) only in the fully loaded class. - Mario_Walke commented on April 3rd 20 at 18:49
April 3rd 20 at 18:45
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 the fullName" + person.fullName);
 System.out.println("This is static fullName" + Person.staticFullName);
}
}
I mean the class initialization that occurs even without creating an object. That object need non-static members is clear. - Aiyana commented on April 3rd 20 at 18:48
"A class can be defined as a template <.. > "object
proglang.su/java/classes-and-objects - Michale commented on April 3rd 20 at 18:51
@Tegan, what's it to you? - nya.Hue commented on April 3rd 20 at 18:54

Find more questions by tags Java