How and where to use Static modifier in Java?

前端 未结 7 2149
走了就别回头了
走了就别回头了 2020-11-28 10:43

How and where should we use a Static modifier for:

1. Field and
2. Method?

For example in java.lang.Math class, the fields

7条回答
  •  粉色の甜心
    2020-11-28 11:16

    Static uses less memory since it exists only once per Classloader.

    To have methods static may save some time, beacuse you do not have to create an object first so you can call a function. You can/should use static methods when they stand pretty much on their own (ie. Math.abs(X) - there really is no object the function needs.) Basically its a convenience thing (at least as far as I see it - others might and will disagree :P)

    Static fields should really be used with caution. There are quite a few patterns that need static fields... but the basics first:

    a static field exists only once. So if you have a simple class (kinda pseudocode):

    class Simple {
     static int a;
     int b;
    }
    

    and now you make objects with:

    Simple myA = new Simple();
    Simple myB = new Simple();
    myA.a = 1;
    myA.b = 2;
    myB.a = 3;
    myB.b = 4;
    System.out.println(myA.a + myA.b + myB.a + myB.b);
    

    you will get 3234 - because by setting myB.a you actually overwrite myA.a as well because a is static. It exists in one place in memory.

    You normally want to avoid this because really weird things might happen. But if you google for example for Factory Pattern you will see that there are actually quite useful uses for this behaviour.

    Hope that clears it up a little.

提交回复
热议问题