JavaRush /Java Blog /Random EN /Modifiers or how to enchant in the Java world
Petr Gorskin
Level 22
Москва

Modifiers or how to enchant in the Java world

Published in the Random EN group
Good galactic time of day, comrades! This is my first interplanetary manuscript, and since I am an engineer, some concepts may be interpreted in an extraterrestrial language.
Modifiers or How to Enchant in the Java World - 1
Introduction In my opinion, Java is like an endless corridor with doors (package). Behind each of these doors there is a free space and a filing cabinet, which contains a mountain of magical scrolls with instructions (classes) for creating objects. Each scroll describes a set of characteristics (variables) and abilities (methods) - actions (fields) performed on either the class itself or the object assembled on its basis. At this point, let's pretend we're wizards and try to create a brand new instruction (class). So: Modifiers or How to Enchant in the Java World - 2Drink the elixir of wisdom (learn the basics) and start writing instructions. Access and non-access modifiers Imagine that any instruction and the object created on its basis, as well as its characteristics and abilities, will be initially magically enchanted, i.e. they will be assigned certain sets of modifiers. Let's try to create a manual on possible enchantments.
  1. 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).

  2. 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.

  3. 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).

  4. 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).

When working in a multi-threaded environment, special modifiers can be used:
  1. 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).

  2. 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.

  3. 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.

Conflicting pairs of modifiers In addition to the applicability of modifiers to different classes, objects, and fields, there is also the concept of conflicting pairs. Let's say enchanting a helmet with durability makes it heavy, and at the same time you want to enchant it for lightness. One does not fit with the other.
  1. 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).
  2. 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).
  3. 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.
  4. Abstract and Syncronized - what's the point of synchronizing work with a method that does nothing?
Summing up So, the description of modifiers is over, all conflicts are sorted out and now you can fix the result with a cheat sheet - an enchantment scheme: Modifiers or How to Enchant in the Java World - 3In conclusion, I want to say that writing this article was the realization of my desire to study modifiers. Successful experience or not - up to you. I'm waiting for your suggestions on how to improve/correct it, and maybe together we'll turn it into a useful manual for beginners.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION