Does java create an object in runtime or in compiletime ?

Another Question :

For embeded system [i.e. mobile phone] that have small memory, what happened if I created large number of objects ? Does JME [Java Micro Edition] uses Garbage Collector to free memory ? and how Virtual Machine knows that this object is dead ?

Recommended Answers

All 10 Replies

I believe that if you want to create objects at runtime you need the reflection api, although I am not certain of this. With regards to JME I am unsure of whether it uses the GC but my guess would be yes. A VM knows when an object is dead when all references to it are gone. Hope this helps!

think about it.
If all object instances were created at compile time, how could you ever create a program that deals with information the amount of which is not known at compile time?

After some research I have discovered that JME does in fact use the Garbage collection algorithm. However, there is no finalize method(to simplify garbage collection) when interacting with objects to prevent object resurrection. HTH

think about it.
If all object instances were created at compile time, how could you ever create a program that deals with information the amount of which is not known at compile time?

So my suspicion is correct? That.....

I believe that if you want to create objects at runtime you need the reflection api

So my suspicion is correct? That.....

No. The point was that all objects are runtime constructs. Classes are created at compile time. This has nothing to do with reflection.

Oh I see, so objects can NEVER be created at compile time simply because there is no need for them to be constructed at compile time?

Correct. An object is an instance in memory of a class. It has no context at compile time because nothing is running. An object by definition is a runtime entity.

OK Thanks for clearing that up, I had thought objects were runtime creatures but read somewhere that reflection may change this......Obviously my source was incorrect!

Reflection allows you to use classes dynamically at runtime in ways that were not specified at compile time by providing methods to query and work with the class internals. The Sun trail on Reflection describes this better than I can here:
http://java.sun.com/docs/books/tutorial/reflect/index.html

Ok Ezzaral thanks a lot for the link, have checked it out and that has cleared up the confusion I had there. I guess my source was confused too or I misread it. Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.