Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why declare variables static

 
Ben Synes
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I understand the creation and use of static variable, in Ruby these are known as class variables, but Im struggling to understand why a static and non static variable for a class is different.

I wrote a simple class, like this



and from another class the same thing can be done too.

Of course static is a fundamental of Java, but can anyone give me a clear indication of when it should be used on variables? I have read the post on static methods (http://www.coderanch.com/t/467894/java/java/declare-method-static) and this makes sense in limiting the use of static, and using it when you dont need access to its data, but how about with variables, does the same apply?

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.

Thanks

Hope this isnt too stupid a question.
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The big difference is in the scope of static variables and non-static ones (also called instance variables):
- a static variable has class scope which means its value is shared among all instances of that class.
- an instance variable has object scope which means its value is bound to only the instance object of the class.

Think a static variable like a public bus which every one can get in, and non-static one like your private car - only you can use it.
 
Winston Gutkowski
Bartender
Pie
Posts: 10243
58
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

The same is also true of Java; and actually applies to programming in general - don't use global variables. And especially, don't use public global variables.

On the other hand, global constants can be quite useful; particularly in terms of documentation. So, if you find yourself creating static fields, make sure you add final as well.

Hope this isnt too stupid a question.

Stupid is in the eye of the beholder.

As they often say on quiz shows: it's only easy if you know the answer.

Winston
 
Ben Synes
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for the replies, I completely understand, its best to consider the use of static variable usage as akin to being a global constant, for that class. As you say, an instance variable is unique to the instance of the class that has been created, but with static, all classes and instances of the class can access it, but there is only ever one value which is anchored to the class where it is defined, that value can be changed, but it will also change for the created instances of it.

And to prove this, when changing the previous code to being just a standard instance variable, this happens:



Thank you very much, really appreciated your replies.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong about creating an instance of your class inside an ordinary method.
Let’s have some information hiding:-
  • 1: Give that field private access.
  • 2: Write a constructor which assigns it an initial value.
  • 3: Reassign it by writing number = 123; or this.number = 123; in that method.
  •  
    Ben Synes
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I completely understand, having reviewed my rather "loose" code, I refactored and here is my result, would this be any better?




     
    devasis majhi
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When we declare a static variable it becomes a class variable and the memory gets allocated in stack memory instead of heap memory. 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. When we declare a data member as static it gets loaded during the time of compilation 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. This way we can save a lot of memory.

     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
    … 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.
    At last, something correct.
    This way we can save a lot of memory.

    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.
     
    devasis majhi
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First of all use proper language when you are addressing on some one's post.

    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.


    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.

    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


    stack memory does not increase automatically where as Heap memory size increases dynamically. Check the llink. link2

    When we declare a data member as static it gets loaded during the time of compilation.
    Nonsense. Nothing is loaded at compilation time.


    Agree with this. This is my mistake. It gets loaded at runtime only.

     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is proper language to call what you posted nonsense. It might not be nice, but you must remember these fora are frequented by beginners who don’t know when they are being told something incorrect.
    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 is completely different from what you said earlier.
    stack memory does not increase automatically where as Heap memory size increases dynamically.
    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.

    Please make sure only to post what will help the beginners here to learn correctly.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic