This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Q: Why did I stop using static after knowing this?

I am a beginner Java programmer. Recently I developed an application using Groovy and Java. All the code I’ve written uses the static keyword quite a bit. I was asked by senior technical staff to reduce the number of static used. I did a Google search and found that many programmers are pretty opposed to using static variables. I find static variables more convenient to use. I find them very efficient (correct me if I’m wrong), because if I have to call 10,000 methods in a single Class, I’m happy to just use static methods and simply call them in the form of class.methodCall (), instead of being flooded with 10,000 instances of classes, right? In addition, using static reduces dependencies on other parts of the code. They can serve as perfect state holders. In addition, I’ve found that static is widely implemented in languages such as Smalltalk and Scala. So why is this annoyance with static so common among programmers (especially in the Java world)? PS: Correct me if I’m wrong about static.

Answer a

The static variable represents global state. This is hard to deduce, and hard to test: if I create a new instance of an object, I can deduce its new state in tests. If I use code that uses static variables, it can be in any state – anything can modify it. I could go on for a long time, but the bigger idea is that the narrower the scope of something, the easier it is to reason. We’re good at thinking about small things, but without modularity, it’s hard to reason out the state of a system of a million lines. By the way, this applies to everything, not just static variables.

Answer two

To say static is harmful is very subjective.

You can’t control the creation and destruction of static objects. They live under the program’s load and destroy commands. Because static objects are held in a class space, all threads that wish to use them must go through access control. This means that programs are very coupled and difficult to imagine and manage once static objects change. This affects the isolation between code blocks and affects how tests are conducted.

Those are the two main problems THAT I think exist.

Answer three

Summarize a few basic advantages and disadvantages of using static methods in Java:

advantages

  1. Globally accessible and not bound to any particular object instance
  2. One instance per JVM
  3. Can be accessed using the class name (no object required)
  4. Contains a value that applies to all instances
  5. Loaded when the JVM starts and stopped when the JVM shuts down
  6. They do not modify the state of the object

disadvantages

  1. Static members are always part of memory, whether or not they are in use
  2. There is no control over the creation and destruction of static variables, which are created when the program loads and destroyed when the JVM shuts down
  3. Thread insecurity is that if one thread changes the value of a static variable, it may break the functionality of other threads
  4. Before you can use an object, you must know that it is static
  5. Unable to override static methods
  6. Serialization doesn’t work on them
  7. They do not participate in runtime polymorphism
  8. If you use a lot of static variables/methods, you have a memory problem because the program is not garbage collected until it is finished
  9. Static methods are also difficult to test

The article translated from Stack Overflow:stackoverflow.com/questions/7…