Reputation: 4287
I searched on google. It says the static field and method are loaded in compile time. In my mind, compiling is used to create the class file and then when executing , the threads are created and the program will occupy the memory. What does it mean "in compile time"? means when creating the class file? http://javarevisited.blogspot.com.au/2012/03/what-is-static-and-dynamic-binding-in.html This is the URL.
Upvotes: 0
Views: 682
Reputation: 32468
I searched on google. It says the static field and method are loaded in compile time. In my mind, compiling is used to create the class file and then when executing , the threads are created and the program will occupy the memory. What does it mean "in compile time"?
As stated by Adriaan Koster in his answer to another question
The compiler optimizes inlineable static final fields by embedding the value in the bytecode instead of computing the value at runtime.
When you fire up a JVM and load a class for the first time (this is done by the classloader when the class is first referenced in any way) any static blocks or fields are 'loaded' into the JVM and become accessible.
Upvotes: 3
Reputation: 3106
I searched on google. It says the static field and method are loaded in compile time.
Static fields and methods are added into memory when the class is loaded by a ClassLoader
.
This is when static blocks are also executed and static fields initialized to a provided or default value.
Also it depends on the version of the JVM what optimisations it does at compile time (inline values, specify to add them to the special place on heap: Permanent Generation) and JIT compiling.
Upvotes: 0
Reputation: 726639
It says the static field and method are loaded in compile time. In my mind, compiling is used to create the class file [...]
You are correct - that's a wrong statement: static methods and fields are resolved at compile time; they could not possibly be loaded at compile time, because your program is not running yet.
What they mean is that the compiler makes a decision about the place in memory from which the static item would be referenced, so when your program is loaded, the access to static members is done without additional calculations. This is called static binding.
In contrast, access to instance members and instance methods is decided at runtime: the location of instance fields in memory depends on the location of the instance, while the location of instance methods depends on the type of the instance.
Upvotes: 5