0

Similar to -Why are static variables considered evil?

I am also a novice programmer in the industry. I am seeing a lot of static variables in all classes declared throughout the application. What is the advantage of making class variables static?

I notice that every class contains at least one static variable.

Example:

public class FileWriter{


    private static Properties configProps = null ;
    private static String propertyFile = "config/database.properties";  
    private static String LOG_PROPERTIES_FILE = "config/log4j.properties";
                    .
                    .
                    .

I understand that these variables will be common for all objects. Say, object1 of FileWriter and object2 of FileWriter are using the same variable configProps created at only one location in the memory?

In the below example, numberA should not be a static variable? What will happen if it is declared static?

  public class sumFinder{

       private int numberA=0;


 }

Please excuse me if it is too silly.

Thanks

Community
  • 1
  • 1
pnv
  • 1,437
  • 3
  • 23
  • 52
  • static and member variables are used as per need basis and not for any advantage. – Juned Ahsan Sep 30 '14 at 11:34
  • 1
    The advantage is that you don't have to actually properly learn Java and object oriented programming to get things to work. Just to be crystal clear: I'm being sarcastic. The truth is in the question you linked to! – Gimby Sep 30 '14 at 11:34
  • Don't be so cruel. Static is handy for constants, like Math.PI. I usually make sure all of my static variables are readonly (final) so other objects can't reassign them. – Davio Sep 30 '14 at 11:36
  • 3
    Take one step at a time. For the time being accept what `static` *means* and with experience you'll learn what it's *good for*. Probably at this point you wouldn't benefit much even if a detailed explanation was given. – Marko Topolnik Sep 30 '14 at 11:36
  • 1
    @Gimby - If you mean *static is bad*, then I disagree. The use of static is more of a design choice. It makes little sense to have a *general* method (which behaves in the same way irrespective of the instance) at an instance level. example : `Integer.parseInt()` – TheLostMind Sep 30 '14 at 11:38

3 Answers3

3

if we declare variable as static we can grantee that there is only one occurrence in the JVM how many times that class instantiated.

Eg:

public class A{
private static int id;
// getter setter
}

Now from main()

A a=new A();
a.setId(2);

When you call some where else a.getId() it will give you 2, how many times A instantiated after set id.

3

Suppose there are 2 girls girl1 and girl2 and they have a common boyfriend named luckyboy.

So the luckyboy can save his time(and memory loss) by going out with both the girls at the same time.So both the girls have an common attribute boyfriend i.e. luckyboy

in the same way static field are common to all the objects created so here boyfriendName attribute(static) is common for the 2 girls and girl1Name and gilr2Name attributes is non-static different for girl1 and girl2 object respectively.

and now think if luckyboy have 1 billion girlfriend then how much memory is saved by taking boyfriendName attribute for each girl Object as static.

So it saves memory

deogratias
  • 472
  • 2
  • 8
  • 23
2

This is the exact words from here:

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

I think there is no advantage of using any thing, but there is need to use some thing. and the same is for static. I hope you know what static variables do, you just have to learn when to use them.

In simple words if you want to use a variable independent of objects and common between all of them, you could use static variables.

Lrrr
  • 4,755
  • 5
  • 41
  • 63