• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3, I think .
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rohit chavan wrote:3, I think .


And I was thinking of 1..
What objects are you making??
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic