Pages

Thursday, August 11, 2011

Avoid unneccassary scope allocation

If you have variables which are not used outside the class, but declared as Public, you should avoid that practice. Declare them as Private. Also, if you have variables which are accesed only with in a particular method, do not declare them in class scope. Define them only in method scope.

Either way we can access the variables to get what we want. Then, why we need to follow this?

Why?

It optimizes memory allocation. For arguments, memory is allocated as stack frames. Once you go out of the scope of variable, the memory allocated for that variable is released. So, more scope you allocate for variables, more memory will be required to hold them.

No comments:

Post a Comment