What is the exact meaning of static fields in Java? -
I would like to share something between different instances of the same class object.
Conceptually, while my program is running, all objects of class A use the same object as square b.
I have seen that static is system-wide and its use is frustrating. Does this mean that if I have another program running, which is running on the same JVM which enters class A objects, then these objects can reach the same B object as it has reached in the previous program. ?
What are the flaws behind using generally stable fields?
Are there any alternatives (for which there is not a huge effort to implement)?
Static means "not shared by all instances" - it means " Not related to the example "In other words, you can get an example without any in class A in the stable field.
To run two programs in the same JVM - what exactly is it that you are running "two programs" means that the stable field with the static object effectively Connected, which is in turn connected with a classloader, so if these two programs use different class loader instances, then you will have two independent static variables if they both use the same classloader, Cave There will be so they will change each other.
For an alternative - there are several options to pass the context of "shared" object to the constructor of each object that you need it. After that the reference will need to be stored later. It can be a bit of a pain and can eat a little more memory than a stable perspective, but it does for easy testability.
Comments
Post a Comment