I am new to Java and recently I have studied about static variables. I got to know that for a static variable memory is allocated only once. This means that it will save a lot of memory.My question is that if static variables saves memory, why not declare every variable as static. This will save a lot of memory while creating an application. Pardon me if this seems a silly question, but actually I am just a bit curious.
-
1Write more programs. You need non-static variables to use objects properly. – Thorbjørn Ravn Andersen May 07 '16 at 09:14
-
1You can find an answer to this question here http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil – Mitiku May 07 '16 at 09:14
-
2If you want to have more than one object of a type e.g. two Strings, with different contents, you can't use static fields. – Peter Lawrey May 07 '16 at 09:19
-
Why not just have one phone number for all of your friends? – Stewart May 07 '16 at 09:25
-
Why stopping in the middle? If our primary goal is saving memory, let's just use a single variable of type `Object` for everything! – Vlad May 07 '16 at 09:31
4 Answers
It's the basics of OOP. Look at an example:
class Person {
public String name = "Foo";
}
Field name
is not static, it means that objects of class Person
will not share it and each person will have it's own name. And when you change one's person name others will stay unaffected. But if you make it static:
class Person {
public static String name = "Foo";
}
It means, that all persons share the same name which is kind of strange, do you agree?)

- 5,294
- 3
- 23
- 43
-
Thank you for your answer. Anyways, I have studied that final keyword in java makes the value of the variable static. That is we cannot change the value of the variable once it is declared, which is pretty much similar to your case. But in case of static keyword, we can change the value of that variable. Only the memory is allocated once. This is what making me confused. – Pratik Paul May 07 '16 at 09:20
The point on creating attributes/variables is that you'll want them as a "feature" for the object. For example, if you have a class "Car", maybe you'll want a variable to reference the color of the car.
The problem is that every instance of "Car" (in real world it'll be each different car) has a value, so each has to be an "independent" variable.
You can use static variables for those ones that are shared by all the objects of this class. For example, a counter to determine how many cars are there. This variable belongs to the class called "Car", but not to any specific instance of that class.

- 775
- 1
- 7
- 17
Static variable are created per class level. It is not created when an Object of the class is created. For every instance or object of the class there is only one value of a static member variable. This defeats the purpose of having objects and creating an application around objects.

- 199
- 9
Yes, it would be allocated memory once in the life cycle and is known as class variable. A class variable can be accessed directly with the class, without the need to create an instance. This would mean that it can be accessed from anywhere and everywhere. Also, memory allocation would mean that even if the variable is not used at many places in the code it would stay in the memory forever as long as the program is running and would take up unnecessary space.

- 579
- 5
- 7
-
small (if any) optimization in this way often break rules of OOP, give poor software quality etc. Correct place to such discussion is general OOP ,not any language. – Jacek Cz May 07 '16 at 09:23