• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many objects are create??

 
vijay mahendra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Rosenberger, yes, you are right. Somebody else quoted a factory method the same time I posted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic