+ 1
Why an object is created only at runtime not at compile time in java?
4 Answers
+ 4
This is just how the language and the JVM was designed.
Compilation transforms your Java code to JVM bytecode.
"Runtime" means when this bytecode is actually executed by the Java Virtual Machine. When objects are created, memory is allocated to them in the computer's memory, and they are kept alive as long as the program is using them.
When the objects are no longer needed, Java automatically frees up the memory, using it's built-in "garbage collection". This is how objects are destroyed. This can be less important for very short programs such as what we can write on Sololearn, but critical for long running processes that might last for hours, days, or even months without ever stopping their execution.
+ 2
Runtime is a period in time where a program runs, with whatever it does. Nothing should be done outside this period of time because the program is no longer running.
I suggest you to edit the post in case you meant to ask something different : )
+ 2
harsh singh
Actually, Tibor Santa had answered your question very well. Compile time is the time when Java compiler compiles the source code into a bytecode, an intermediate format that JVM can process (run) later on.
At compile time, the bytecode is not executed yet, so no object was created.
After the compilation into bytecode complete, then the bytecode is loaded and processed by JVM to run. At this stage, the code is actually run.
Thanks for editing to clarify ...
+ 1
Ipang i'v edit the question... pls look forward to it.