Why an object is created only at runtime not at compile time in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why an object is created only at runtime not at compile time in java?

7th Jan 2023, 7:18 AM
harsh singh
harsh singh - avatar
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.
7th Jan 2023, 8:04 AM
Tibor Santa
Tibor Santa - avatar
+ 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 : )
7th Jan 2023, 7:51 AM
Ipang
+ 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 ...
7th Jan 2023, 9:07 AM
Ipang
+ 1
Ipang i'v edit the question... pls look forward to it.
7th Jan 2023, 8:32 AM
harsh singh
harsh singh - avatar