• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What are the number of objects created in this sample program?

 
Singh Kuldeep
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test{
String abc;

public static void main(String []args){

}
}


Please some body tell number of object actually created in the above program.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singh Kuldeep wrote:

Use CodeTags for your code..
Singh Kuldeep wrote:
Please some body tell number of object actually created in the above program.


What is your thought on that??
 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3, I think .
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit chavan wrote:3, I think .

And I was thinking of 1..
What objects are you making??
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singh Kuldeep wrote:Please some body tell number of object actually created in the above program.

0...
I think you have to get brief introduction about Reference and Object.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote:
Singh Kuldeep wrote:Please some body tell number of object actually created in the above program.

0...
I think you have to get brief introduction about Reference and Object.

Oh yes.. it is indeed 0.. I forgot that we are not passing any command line argument and the args array will be initialized to null..
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote: . . . the args array will be initialized to null..
No it isn’t. Try this:
System.out.printf("args == null? %s%n", args == null ? "yes" : "no");
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides, in run-time under the hood a plethora of objects will be created by the JVM, albeit they are not directly accessible from the user code.
 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:
rohit chavan wrote:3, I think .

And I was thinking of 1..
What objects are you making??


I think even I mixed reference and object here, but there sure will be one object of class Class for the class test

Other objects I was thinking about and of which I guess first one won't create an object.

So is 2 the answer?

(and one more thing why test isn't Test here? )
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit chavan wrote:
I think even I mixed reference and object here, but there sure will be one object of Class class for the class test

If you mean object of class test by this statement then I'm afraid I don't see any..

rohit chavan wrote:
Other objects I was thinking about and of which I guess first one won't create an object.
So is 2 the answer?

So removing the test object.. there's only 1... (that of String args[])
But there might be more.. See the post previous to yours for exact answer..

 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:
rohit chavan wrote:
I think even I mixed reference and object here, but there sure will be one object of Class class for the class test

If you mean object of class test by this statement then I'm afraid I don't see any..

rohit chavan wrote:
Other objects I was thinking about and of which I guess first one won't create an object.
So is 2 the answer?

So removing the test object.. there's only 1... (that of String args[])
But there might be more.. See the post previous to yours for exact answer..


I meant Object of class Class as they are referring to in below piece of code taken from http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html.

 
s ravi chandran
Ranch Hand
Posts: 372
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
won't the string abject also be counted?



It would also be initialized to default null, right? hence, it should also be counted.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:won't the string abject also be counted?



It would also be initialized to default null, right? hence, it should also be counted.

First, since the object of class test itself has not been made.. There is no point in counting String abc for object creation..
Second, even if we make object of class test, and String abc is initialized to default value null.. It is not creating an object of type String..
Because, String abc = null, simply means a null reference .. So no object created..
 
s ravi chandran
Ranch Hand
Posts: 372
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya. That would be right. I am getting dumber with age. One small doubt, suppose I keep the main method blank, and run the class. It would create the object of that class, right? Even though it won't run the main thread as main is empty body.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:It would create the object of that class, right?

Instead of asking me.. Just think upon it yourself..
You are saying main is empty and you are also asking whether it would create object of a class.. Aren't these statements contradictory??

s ravi chandran wrote:Even though it won't run the main thread as main is empty body.

You don't run the main thread manually, it is done by the JVM, the very first time you run your Java Class containing main.
It is from this thread only, other threads are spawned..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic