• Post Reply Bookmark Topic Watch Topic
  • New Topic

static functions  RSS feed

 
Adi Sharma
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I was going through the static methods. It says that for static variables, there is only one copy for the entire class. Is it the same with the methods as well?
I mean that if a method is defined to be static , there is only one copy of it in the memory no matter how many objects of the same class are made and they all access the same one method in the memory.

Thanks a Lot

Aditya sharma
 
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
Adi Sharma wrote:Hi ,

I was going through the static methods. It says that for static variables, there is only one copy for the entire class. Is it the same with the methods as well?
I mean that if a method is defined to be static , there is only one copy of it in the memory no matter how many objects of the same class are made and they all access the same one method in the memory.

Thanks a Lot

Aditya sharma


There is no reason to copy a method, regardless of whether the method is static is or not. It will be incredible code bloat, if you need to make a copy of the methods for each instance.

The main difference between a static method and an instance method, is that an instance method gets called with a reference of the instance used to call the method (the "this" object).

Henry
 
Adi Sharma
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

I think you got me wrong. First of all, I don't want to copy a method.

All that I was saying was that for a static variable is allocated memory only once even if multiple objects are created for that class in which it was declared. Is it the same with the static methods as well?

Thank You
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure, but I think:

The methods live in the Class object, so methods are not usually copied into the individual objects.

That was a bad explanation, but there is a better one in the "manual" for the BCEL project.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!