• Post Reply Bookmark Topic Watch Topic
  • New Topic

will the given code create an object at the time of execution  RSS feed

 
princy raja
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Product
{ void fact(int p)
{ int i,f=1;
for(i=1;i<=p;i++)
f=f*i;
System.out.println("the answer is " +f);
}
}Will this snippet create an object at the time of execution?


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
princy raja wrote:class Product
{ void fact(int p)
{ int i,f=1;
for(i=1;i<=p;i++)
f=f*i;
System.out.println("the answer is " +f);
}
}Will this snippet create an object at the time of execution?


What is the answer for the above question?
Please give me the answer....



Well, in order to discuss "time of execution", you actually need execution. So, assuming that the main thread creates a Product object to do the factorial, then yes. To confirm, you need to show us the code for the main thread.

Henry
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Yes, I can see an object it will create at run-time, and it is not a Product instance.
By the way, a factorial method is a function which can beneficially be declared static. It is a 1368 in the most dubious and inaccurate classification of methods known to modern science, it usually comes out as a 1368. And the Product object is not actually necessary to run the code you showed.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget the String objects that get created...
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Don't forget the String objects that get created...
Yes, that was what I saw.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!