aspose file tools*
The moose likes Java in General and the fly likes What are the number of objects created in this sample program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What are the number of objects created in this sample program?" Watch "What are the number of objects created in this sample program?" New topic
Author

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

Singh Kuldeep
Greenhorn

Joined: Jul 18, 2012
Posts: 19
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

Joined: Aug 11, 2012
Posts: 375
    
    1

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

Joined: Oct 08, 2010
Posts: 132

3, I think .
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

rohit chavan wrote:3, I think .

And I was thinking of 1..
What objects are you making??
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
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

Joined: Aug 11, 2012
Posts: 375
    
    1

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

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: May 22, 2012
Posts: 877
    
    5
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

Joined: Oct 08, 2010
Posts: 132

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

Joined: Aug 11, 2012
Posts: 375
    
    1

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

Joined: Oct 08, 2010
Posts: 132

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

Joined: Dec 20, 2010
Posts: 178
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

Joined: Aug 11, 2012
Posts: 375
    
    1

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

Joined: Dec 20, 2010
Posts: 178
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

Joined: Aug 11, 2012
Posts: 375
    
    1

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..
 
jQuery in Action, 2nd edition
 
subject: What are the number of objects created in this sample program?