wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how many objects are create?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how many objects are create??" Watch "how many objects are create??" New topic
Author

how many objects are create??

vijay mahendra
Ranch Hand

Joined: Jan 01, 2007
Posts: 51
Hi ,

class a {}
class b extends b{ //main method()
}

this program compiled and executed. Totally how many objects are created internally ??
and is there any posiblity to get objects count in JVM?

please give suggestions.
thanks.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Vijay,


class a {}
class b extends b{ //main method()
}



In Java there are two ways to create objects in the heap.

1- ClassName obj = new ClassName();
2- ClassName obj = ClassName.class.newInstance();

When the program is run, it is sure that the class is load by the JVM which consists the main() method and it is up to the JVM to decide which other classes are associated and should be loaded with it at that moment. Suppose you are creating an instance of class X in class Y, class X will be loaded too. You extends class "a" in class "b", class "a" will be loaded too with class "a".


Thanks and Regards,
cmbhatt


cmbhatt
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
Count how many times you have said "new" in your coding.
Add how many String literals you have have; "Campbell" is an object, and "Campbell" "Campbell" is also one object.
Count how many times you have used an array initialiser "int[] array = {1, 2, 3};"

That many objects have been created. If you only have a main method which doesn't say "new" you might not have created any objects.

There is no simple way of getting object counts from the JVM; there may be hacks, which you can find by searching the fora, since this question comes up about once a week!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
You can place a static counter in the constructor that increments each time an instance is created.

When you use a String literal in your program, this String literal goes on the String constant pool not on the heap. Only the "new" makes an object to be created on the heap at run time.


Thanks and Regards,
cmbhatt
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Campbell, you may be missing a few ways. factory objects can create other objects, without a new (this assumes the factory is a third party object where you can't look at the source). Many of the String methods create new string objects, as does String concatenation.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
Fred Rosenberger, yes, you are right. Somebody else quoted a factory method the same time I posted.
 
 
subject: how many objects are create??
 
Similar Threads
how many object created ?
Hibernate 2 deleting by cascade
Inheritance - how many objects created???
obiect/class/method
creation of object using new operator