No. One object is created every time the new operator is used and then the constructor runs. So you can have many instances of a class with a default constructor.
krishnadhar Mellacheruvu wrote:. . .
1) will the default constructor create only one object for a class
As many as you have space to fit them in
2) How many references can an object have
Usually yes, that is the definition of unreachability.
. . .
5) is it not that object becoming unreachable mean that there are no referencing variable to it...
Variables are not created on the heap; they are local variables on the stack or they are fields inside the code representing an object. That latter is usually on the heap.
. . .
6) How can a reference variable be created on a heap and if it is created what would be its status as it is the object only that is garbage collected.
krishnadhar Mellacheruvu wrote:
all the while i was thinking that by doing MyClass1 mc1 = new MyClass1() i am creating an object, but nopes i was wrong, by using that statement i was just creating a reference variable to point to the object created by the default MyClass1 constructor.
krishnadhar Mellacheruvu wrote:1) will the default constructor create only one object for a class
krishnadhar Mellacheruvu wrote:2) How many references can an object have
krishnadhar Mellacheruvu wrote:here default constructor of the class MethodDemo1 creates an object which is being referenced by md reference variable. in the above case when will the object be destroyed i.e. garbage collected
krishnadhar Mellacheruvu wrote:4) Does for every execution of the program an object is created in the heap memory of the program
krishnadhar Mellacheruvu wrote:5) is it not that object becoming unreachable mean that there are no referencing variable to it...
krishnadhar Mellacheruvu wrote:6) How can a reference variable be created on a heap and if it is created what would be its status as it is the object only that is garbage collected.
Probably never. The “GC” tool only runs when there is a risk of running short of heap space, and that is highly unlikely in the case of a small program like yours. You should not worry about when the GC tool runs, nor about how it does it, but simply remember that it does it automatically. The GC tool can do things automatically far better than any programmer can program and is much less error‑prone. Forget all about it. As long as you don't have any uncollected and unwanted references somewhere. An example of an unwanted reference would be on a stack, where you push a reference and forget to delete it after it has been popped.
krishnadhar Mellacheruvu wrote:. . .. . . in the above case when will the object be destroyed i.e. garbage collected
Simple answer: No. You create "Robert", "Hi ", " your age is:", "Hi Robert your age is:30", args(array) and one MethodDemo object. They are on the heap. And there are other objects behind the scenes, e.g. a StringBuilder, which you do not see. Again on the heap.
4) Does for every execution of the program an object is created in the heap memory of the program
. . .
Those questions look like something out of a certification book. If so, please tell us which book (and page number).
String names = new String;
Arrays.fill(names, "Campbell"); // 10000000 references inside the same array