And what static restrictions will mean besides:

  1. Impossibility to use this ,

  2. That if the variable is marked as static then it is one for all instances (actually because the static variable is not tied to any object).

Are there any other limitations when using the static modifier which I did not name or which obviously do not follow from those that I called?

    1 answer 1

    1) You can NOT access non-static members of a class, within a static context, as an option, method or block.

    2) Unlike local variables, static fields and methods are NOT thread Thread-safe in Java. In practice, this is one of the most frequent causes of problems related to the security of multi-threaded programming.

    3) Static methods have the advantage of using, because there is no need to create a new object each time to access such methods. A static method can be called using the class type in which these methods are described.

    4) Another important point is that you can NOT override ( Override ) static methods.

    5) You can also declare static a class, with the exception of top-level classes. Such classes are known as “nested static classes”. They are useful for presenting improved connections.

    6) The static modifier can also be declared in a static block, better known as the “ Static initializer block ”, which will be executed during class loading.

    7) It is useful to know that static methods are linked at compile time, as opposed to binding virtual or non-static methods that are linked at run time on a real object. Therefore, static methods cannot be redefined in Java, since runtime polymorphism does not apply to them.

    8) Static fields or variables are initialized after the class is loaded into memory.

    9) Static fields are not serialized.

    10) And finally, let's talk about static import . This modifier has a lot in common with the standard import statement, but unlike it, it allows you to import one or all static class members.

    Sources

    • 2
      And the link to the original article is not necessary? :-) Also painted with it :-) but did not have time - Evgenii Izhboldin
    • Of course it is necessary! ;-) - Senior Pomidor
    • Point 2 is weird. In terms of thread safety, non-static fields and methods are no different from static ones. It is not clear why they are contrasted with local variables. - Roman
    • As a consequence of clause 7, calling a method static is cheaper - Artem Konovalov