-
An access modifier that describes the accessibility of a class, object, or field is mandatory: in its absence, the JVM (this is such a source of universal magic) automatically assigns a default identifier package.
Starting from the second modifier, there are non-access modifiers that are not required and may also conflict with each other (but first things first).
-
The Static modifier says that the class, object, or field is static. But what does it mean? Basically, it is suitable for fields.
Static variables are called class variables and are unique across all instances of a given class. Static methods can be called without creating the object in which they are declared. Static classes are used when nesting one class into another, and the principle of interaction between the inner class and the outer class is similar to methods (this is a topic for a separate discussion). It can also be used as a separate block within an object.
-
The Final modifier actually indicates to a variable that it is a constant. For methods - that they cannot be overridden when inherited, but for classes, this is an indication that it is impossible to inherit from it (immutable).
The static and final modifiers apply to classes, objects, and fields. However, there are modifiers that apply only to some of them (or even parts of them, because both a variable and a method are a field, but not all modifiers apply to both). If we draw an analogy, only boots and gloves can be enchanted for speed (to move faster), but it makes no sense to enchant a hat for speed (and the Ecumenical Oversight Council (compiler) will not give it).
-
The Abstract modifier applies only to methods and classes . An abstract method is a method without an implementation (body).
If a class is marked as abstract, it either contains abstract methods, or it is done to prevent the creation of instances of this class. If we draw an analogy, in the middle of the instruction you can see the title "Coloring an object", after which there is no description. Those. according to this instruction, you can create an object and you can also color it, but this instruction does not specifically say how (write your instructions for creating a red object based on this object and describe how to color it).
-
The Syncronized modifier is only used for methods. Its presence indicates that only one thread can execute it at a time. It can also be used as a separate block of code within an object (with an indication of the synchronization object).
-
The Volatile modifier is for variables only . It is recommended to use it for variables that can be used by multiple threads at the same time. A variable with such a modifier is instantly copied from the processor cache to main memory with each change, allowing parallel threads to get the "freshest" value.
It is worth noting that volatile is applicable in cases where only one thread can write to a variable, and the rest can only read from it. For other cases, it is better to hang the synchronized modifier on methods that write to a volatile variable.
-
The Transient modifier is for variables only . Such a modifier marks variables that need to be skipped when serializing an object (this is such a smart process, which, in principle, you can read about on your own ...) *
* - this article was written by me at level 17 and until then serialization as a process was not described, therefore, instead of copy-paste, I advise, if necessary, to study it yourself.
- Final and Volatile - when it comes to variables, we cannot simultaneously say that it is final (constant) and that several threads have the ability to change it ... After all, it is constant, and at any moment a thread can read it, but not a single thread can change the constant (the Ecumenical Oversight Council will not allow).
- Final and Abstract - classes and methods cannot be both abstract (which in most cases implies the need to refine them for implementation) and final, i.e. immutable. It turns out that the instructions say how to create a good durable helmet from any material (abstract part), but for this it must not have holes in it (the final mandatory part, cannot be changed).
- Abstract and Static - An abstract method cannot be static or synchronized at the same time. A static abstract method does not make sense, because not only does it not do anything, it also belongs to the whole class - it turns out to be a useless thing.
- Abstract and Syncronized - what's the point of synchronizing work with a method that does nothing?
GO TO FULL VERSION