Click here to Skip to main content
15,436,765 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In the given code , the object s is created.
Is it created in compile time or run time ?
Can we create objects at compile time?

String s = "hello world" ;

What I have tried:

Searched google .....................................
Updated 19-Jan-22 19:59pm

1 solution

That's a complicated one, because strings are a special case: Interning of String in Java - GeeksforGeeks[^]

But for all other reference types, the answer is "run time only" - the compiler does not execute your code, so no object instances are actually created.
For example, if your class constructor always creates a file on disk, the file will not exist until your app has been compiled successfully, executed, and the code creates an instance of that class.
Share this answer
CPallini 20-Jan-22 2:14am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900