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

Modifiers or how to cast enchantments in the Java world

Published in the Random EN group
Good galactic day, comrades! This is my first interplanetary manuscript, and since I am an engineer, the interpretation of some concepts can be done in an extraterrestrial language.
Modifiers or how to cast enchantments 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 free space and a file cabinet in which a mountain of magical scrolls with instructions (classes) for creating objects is stored. Each scroll describes a set of characteristics (variables) and abilities (methods) - actions (fields) performed on either the class itself or an object assembled on its basis. At this stage, let's imagine ourselves as wizards and try to create a completely new instruction (class). So: Modifiers or how to cast enchantments in the Java world - 2Let's drink the elixir of wisdom (learn the basics) and start writing instructions. Access and non-access modifiers Let's imagine that any instruction and an object created on its basis, as well as its characteristics and abilities, will initially be magically enchanted, i.e. they will be assigned specific sets of modifiers. Let's try to create a guide on possible enchantments.
  1. 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).

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

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

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

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

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

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

Conflicting Modifier Pairs In addition to the applicability of modifiers to various classes, objects, and fields, there is also the concept of conflicting pairs. Let's say that enchanting a helmet with strength makes it heavy, and at the same time you want to enchant it to be light. 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 Universal Supervisory Council will not allow it).
  2. 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).
  3. 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.
  4. Abstract and Syncronized - what's the point of synchronizing work with a method that doesn't do anything?
Summing up So, the description of modifiers is over, all conflicts have been sorted out and now you can consolidate the result with a cheat sheet - an enchantment diagram: Modifiers or how to cast enchantments in the world of Java - 3In conclusion, I want to say that writing this article was the realization of my desire to study modifiers. Whether the experience is successful or not is up to you to judge. I'm waiting for your suggestions for improving/correcting it and, perhaps together, we'll make it into a very useful manual for beginner java players.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION