Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Why declare variables static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Why declare variables static" Watch "Why declare variables static" New topic
Author

Why declare variables static

Ben Synes
Ranch Hand

Joined: Jul 18, 2012
Posts: 54
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

Joined: Oct 31, 2011
Posts: 498

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.


PM Me If You Want to get Urgent Help on Java Programming
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ben Synes
Ranch Hand

Joined: Jul 18, 2012
Posts: 54
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

    Joined: Jul 18, 2012
    Posts: 54
    I completely understand, having reviewed my rather "loose" code, I refactored and here is my result, would this be any better?




    devasis majhi
    Greenhorn

    Joined: Dec 29, 2012
    Posts: 9
    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

    Joined: Oct 13, 2005
    Posts: 39084
        
      23
    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

    Joined: Dec 29, 2012
    Posts: 9
    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

    Joined: Oct 13, 2005
    Posts: 39084
        
      23
    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.
     
    GeeCON Prague 2014
     
    subject: Why declare variables static