• Post Reply Bookmark Topic Watch Topic
  • New Topic

What determines the time required to create an object ?  RSS feed

 
Alok Pota
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What deteremines the time required to create a simple Java object?
-Do the number of member variables affect object creation? Is there a difference between a class with no instance variables
defined and a class with 10 member variables.
-Do the accessors/mutators declared and additional methods affect
object creation? i.e., Do extra methods defined on the class slow down object creation?
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Haggar's book Practical Java goes through the process of object creation. Paraphrasing from his book
1. memory is created on the stack
2. instance variables are initialized to default vaules
3. static intializers are run
4. constructors are run
3-4 start from the root of the inheritence tree (i.e. Object) and go down the chain.
The more ancestors, the more time. If the class is sufficently large, allocating memory on the heap may take more time, because it needs to be allocated from the system (although this is not the common case). The more complex the initializers and constructors, the more code that must be executed. All these issues may vary by JVM implementation.

--Mark
hershey@vaultus.com
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!