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.
-
2Please provide a link to where you read that. – Oliver Charlesworth Apr 13 '14 at 14:09
-
This article seems to be about binding, not memory management. Can you quote fragment which makes you think that "*static field and method are loaded in compile time*"? – Pshemo Apr 13 '14 at 14:14
-
private, final and static methods and variables uses static binding and bonded by compiler Read more: http://javarevisited.blogspot.com/2012/03/what-is-static-and-dynamic-binding-in.html#ixzz2ymAM7JmU I just found this when I read about the static binding. So I was confused.. – hidemyname Apr 13 '14 at 14:16
-
Your link does not contain the words 'loaded in compile time'. Your question is therefore pointless. – user207421 Apr 16 '14 at 11:51
3 Answers
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.

- 714,442
- 84
- 1,110
- 1,523
-
This answer is really clear. Thx! I think I've understood more. seems I mistakenly understood that sentence. – hidemyname Apr 13 '14 at 14:20
-
Another quetion...So the reason why non-static field can't be resloved in the static method is because their locations are different? – hidemyname Apr 13 '14 at 14:25
-
No. It is because static methods can exists outside of a class instance while non-static fields are bound to a class instance. – Andrei Apr 13 '14 at 14:31
-
@deathlee In order to access non-static (AKA "instance") fields you need to tell Java the object instance on which you are accessing that field by giving an object reference. You definitely can do it in a static method; however, unlike non-static methods where you get the instance "for free", you must specify the instance explicitly in the static method. – Sergey Kalinichenko Apr 13 '14 at 14:32
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.

- 1
- 1

- 31,165
- 11
- 75
- 105
-
2When you're referencing parts of another answer or an external source, please add them in blockquotes. Check the [Help Center page on *Referencing* for more details](http://stackoverflow.com/help/referencing). – Amal Murali Apr 17 '14 at 04:54
-
@AmalMurali : Cool, will do it in future. Anyway, I have added the link of the original answer – Abimaran Kugathasan Apr 17 '14 at 04:58
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.

- 3,086
- 2
- 19
- 24