static vs non static

后端 未结 4 1749
梦谈多话
梦谈多话 2020-12-19 04:21

Until few weeks back, I thought I understand when to make fields and methods static or non-static. For example, when a field (say object of another

相关标签:
4条回答
  • 2020-12-19 04:30

    It might be thin but there is very clear distinction. You declare a field as static when it is not at all related to any instance of a class.

    A simple usecase of static field is declaring constants using final keyword, e.g.:

    public static final int MAX_ALLOWED = 10;
    

    Same is the case with methods also. You declare a method as static when it is not dependent on instance of a class OR the state of the class. That's the reason a static method cannot use instance members of a class.

    0 讨论(0)
  • 2020-12-19 04:35

    These are not mandatory rules, but it is better to follow them:

    When to use static fields:

    1. Constants Best usage of static fields are constants, which shall be final too.
    2. ThreadLocals Most of the time you define thread-local variables static, otherwise you usually lose the reference to them. But don't forget to release their contents when there is no more use (or you will encounter memory leaks). Most of the time these variables are final too.
    3. And very rare cases when you shall keep a reference to a (semantically) singleton object which could be accessed from many places. (For example a reference to Hibernate SessionFactory in HibernateUtils).

    Remember that, static fields shall usually be immutable during runtime of your application (they are sometimes modified during start-up and tear-down of the application).

    When to use static methods:

    1. Helper utility methods that's the normal case, when a method does not depend on state of its containing object. These methods can be useful, and can be accessed without instantiating any object.
    2. Factory methods Factory methods are another good example of where to use static methods.
    3. Private methods which are not depend on objects state, when a private method just have no dependency on object's state, it is much better to define it static to gain a little performance.
    4. void main(String[]) method shall be static too.
    0 讨论(0)
  • 2020-12-19 04:35

    Static fields is a concept you should really think carefully about before using it as a method to increase the efficiency of your application.

    As you know when static modifier is included in a field , no instance of the class is required for it to be used. And as a result it has a the same value for the entire application. On one hand it can lead to many bugs in multi-threaded read/write environment if you don't properly serialize the access, on the other hand it is good thing if you trying to create singleton pattern (A field with a value that do not change during the life time of the application and therefor do not need to be GC)

    By and large you should avoid read/write static fields, it will introduce more bugs into your application. Having the same value across many instances of the same class is not considered a good use case for static field in OO design. Not because it is less or more efficient but because it breaks the concept of encapsulation.

    0 讨论(0)
  • 2020-12-19 04:44

    Fields are not made static over garbage collection. That is whole new area to consider. If the memory is huge for your static field, yeah you should make it non-static.

    0 讨论(0)
提交回复
热议问题