Ben Synes wrote:In Ruby, there is a string emphasis on not using class variables, they are seen somewhat as constants and the value being unchanged, however, the value can be changed but this should only be done as a last resort.
Hope this isnt too stupid a question.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Nonsense. A static member is probably to be found inside the Class object.devasis majhi wrote:When we declare a static variable it becomes a class variable and the memory gets allocated in stack memory instead of heap memory.
Nonsense. There is nothing static about the stack. The stack changes far more frequently than the heapTo put a light on stack and heap memory, stack memory is the static memory where class gets loaded and heap memory is the dynamic memory where objects get created.
Nonsense. Nothing is loaded at compilation time.When we declare a data member as static it gets loaded during the time of compilation.
At last, something correct.… along with the class loading. All the instances of the class will point to the same reference of static field. Hence if we change the value of static data member it gets reflected for all the objects.
For a better understanding say you have a Citizen class with below data members.
public class Citizen
{
private String country = "INDIA";
private String uid;
private int age;
private char sex;
}
In this case the country will be "INDIA" for all the citizens. If we will not make it static all the instances of Citizen class will have a copy of country = "INDIA" . If we will make it a static variable then only one reference will created and each object will have the same reference for country.
Memory saving is the last thing you should worry about, because memory is cheap. Very cheap. But declaring a member static is nothing to do with memory; it is a specific design decision. If you don’t know why you are declaring something static, assume the static bit is a mistake. If you find you have to declare something static so the compiler doesn’t complain, that usually signifies a mistake elsewhere: something else incorrectly marked static.This way we can save a lot of memory.
Campbell Ritchie
Today 15:17:41 Subject: Why declare variables static
devasis majhi wrote:
When we declare a static variable it becomes a class variable and the memory gets allocated in stack memory instead of heap memory.
Nonsense. A static member is probably to be found inside the Class object.
To put a light on stack and heap memory, stack memory is the static memory where class gets loaded and heap memory is the dynamic memory where objects get created.
Nonsense. There is nothing static about the stack. The stack changes far more frequently than the heap
When we declare a data member as static it gets loaded during the time of compilation.
Nonsense. Nothing is loaded at compilation time.
That is completely different from what you said earlier.By this what I mean is a static variable is not associated with the object of the class rather it is associated with the class itself.
That may be true but it has nothing to do with the question at present, and is again completely different from what you posted earlier.stack memory does not increase automatically where as Heap memory size increases dynamically.