There is nothing static (unchanging) about them. They don’t cling. They are perfectly clear, unlike radio signals garbled by static.
They are allocated when the class is loaded. static refers to a method or variable that is not attached to a particular object, but rather to the class as a whole.
static final when applied to a variable is Javanese for constant. All static methods are automatically final. It is not strictly speaking an error to mark them final, but it is redundant and considered bad form.
static methods work without any this object. static methods are limited to calling other static methods in the class and to using only static variables. They can call instance methods only if they use their own object references — not rely on this..
static methods and variable are in a sense inherited, but not in the same strong sense that instance variables and methods are. You can refer to Dog.bark() as Dalmatian.bark() if no one has written a Dalmatian.bark(). However, if you use Dog.bark() you always get the Dog version and if you say Dalmatian.bark() you always get the Dalmatian version.
Newbies tend to overuse static variables. Consider what would happen if your code were used by several threads simultaneously. With shared static variable they would trip over each other. With local and instance variables they often would not, even without any special synchronisation. Sometimes, of course, you do need the globalness of static variables, but don’t use it where it would make more sense to create a object to track each chain of calculation.
Finally, Java has an obscure feature where you can also declare nested classes static. This does not mean all their methods are static, or that they cannot be instantiated, rather it means you instantiate the inner classes independently of the main enclosing class. There is no associated outer class object. Such classes are often called nested static classes. Non-static inner class objects always have an associated outer class object.
You can execute code to initialise static variables in a static init block. You can also allocate local variables whose scope is the block, but not static or instance variables.
I have discovered a Java gotcha. I don’t recall ever reading about it, though it is documented in the JLS (Java Language Specification). It make perfect sense though it completely baffled me for hours.
Every time I executed a static method in one class, no matter how trivial, synchronized or not, it would just hang. IntelliJ trace would just go to sleep.
I was doing threading and I had some locks internal to two ConcurrentHashMaps in the class, but they should lock on the Collection, not the whole class.
I turned on -verbose mode and discovered some system locking code was being loaded just prior to the freeze.
I think what happens is this. static init code does a behind-the-scenes lock of the class object. You thus cannot call any methods of the class on a separate thread until the static init code is complete. My static init code was spawning threads which tried to call the class’s static methods.
The rule of thumb is, don’t use static init so fire up complicated code, just use it to init a few fields.
This page is posted
Optional Replicator mirror
Your face IP:[22.214.171.124]
You are visitor number|