-
The access modifier , which describes the accessibility of a class, object or field, is required: in its absence, the JVM (this is such a source of universal magic) automatically assigns a default identifier to package.
Starting from the second modifier there are non-access modifiers, which are not mandatory and can also conflict with each other (but first things first).
-
The Static modifier indicates that the class, object, or field is static. But what does it mean? Basically we will try it on the fields.
Static variables are called class variables and are unique to all instances of that 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 one is similar to methods (this is a topic for a separate discussion). Can also be used as a separate block within an object.
-
The Final modifier effectively indicates to a variable that it is a constant. For methods - that they cannot be overridden during inheritance, but for classes this is an indication that it cannot be inherited 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 part of them, since 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 make them move faster), but enchanting a hat for speed is pointless (and the Universal Supervisory Council (compiler) will not allow it).
-
The Abstract modifier only applies 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 is done to prevent the creation of instances of that class. If we draw an analogy, in the middle of the instructions you can see the heading “Coloring an object”, after which there is no description. Those. using this instruction you can create an object and you can also color it, but this particular instruction does not say how (write your own instructions for creating a red object based on this object and describe how to color it).
-
The Syncronized modifier is used only for methods. Its presence indicates that only one thread can execute it at a time. Can also be used as a separate block of code within an object (specifying the synchronization object).
-
Volatile modifier - only for variables . It is recommended to be used for variables that can be used simultaneously by multiple threads. A variable with such a modifier is instantly copied from the processor cache to main memory every time it changes, allowing parallel threads to receive the most recent 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 add the synchronized modifier to methods that write to a volatile variable.
-
Transient modifier – only for variables . This modifier marks variables that need to be skipped when serializing an object (this is such a smart process that, in principle, you can read about it yourself...)*
* - I wrote this article at level 17 and until then serialization as a process had not been described, so instead of copy-pasting, I advise you to study it yourself if necessary.
- 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 Universal Supervisory Council will not allow it).
- Final and Abstract - classes and methods cannot be both abstract (which in most cases implies the need to clarify 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 there must be no 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 makes no sense, because not only does it not do anything, but it also belongs to an entire class - the result is a useless thing.
- Abstract and Syncronized - what's the point of synchronizing work with a method that doesn't do anything?
GO TO FULL VERSION