JavaRush /Java Blog /Random EN /Differences between constructors and ordinary methods.
fog
Level 18

Differences between constructors and ordinary methods.

Published in the Random EN group
A constructor is a special method that is intended to initially set the values ​​of an object's fields. At first glance, object constructors do not differ much from ordinary object methods. And indeed, inside the constructor we can do everything we can do in regular object methods: output text to the console, access all fields and methods of the new object, throw exceptions, and so on. Just like regular methods, constructors can have arguments. Just like overloaded methods, there can be several constructors with different signatures. Just like generic methods, constructors can be parameterized by type variables. Even if we look into the bytecode generated by the compiler, in the place where there should be a call to the constructor, we will find a call to some method with the name of <init>which the call is no different from the call to other methods of the object. And having found the bytecode of this method, we will find that it contains the result of compiling our constructor. It seems that there are not many differences from conventional methods, but they exist, and quite significant ones. First, let's figure it out, why do we actually need constructors? To store and process any data, be it primitive types, arrays, or objects, we need a certain amount of memory. This can be processor registers, stack space, or a piece of space allocated in the process data section, or in a dynamically allocated part of memory (heap). In many programming languages, for speedup purposes, when a program requests a new piece of memory, the memory was given to the program not cleared, and could contain arbitrary data that was stored in this memory cell earlier. Preparing and writing the necessary values ​​into such a piece of memory so that in the end there would be some meaningful data structure there fell entirely on the shoulders of the programmer. Quite naturally, programmers wanted to make their lives easier and wrote routines to initialize (that is, set initial values) for frequently used data structures. Such routines were used almost constantly, so the creators of the Java language decided to make such initialization routines mandatory to call when creating objects, and called them constructors . When a new object is created in Java, the following happens: First, the Java memory manager allocates the amount of memory necessary to accommodate the object. In this case, not only the fields declared directly in the class of the created object are taken into account, but also the fields declared in all ancestors of this class. Additionally, this volume includes space for placing structures that are used by the Java machine for internal needs. All fields of such a “blank” are automatically set to default values ​​- nullfor reference types, 0for numbers and falseforboolean. After this, the class constructor is automatically called, whose task is to set the initial values ​​of the object’s fields. While in a normal method the first statement can be anything, the constructor has much less freedom. The first statement of a constructor must be either an explicit call to another constructor of the same class, or an explicit or implicit call to a constructor of a parent class. Explicit calls to constructors of the same class are made using a keyword thisfollowed by a set of arguments enclosed in parentheses. Calling the constructor of a parent class explicitly is done in exactly the same way, but the keyword is used super. In the arguments of an explicit call to the constructor of the same or parent class, you cannot access the fields and methods of the object, as well as use the keywords thisand super, since an explicit call to the constructor introduces a static context. To implicitly call the constructor of a parent class, you do not need to write anything, but the default constructor is implicitly called, which must exist and be visible to the current class. At the same time, it should be borne in mind that if the chain of calling parent constructors is interrupted before the class constructor Objectlocated at the top of the chain successfully completes its work, then the object will not be finalizable, that is, the method finalize()of such an object will never be called. After the parent class constructor completes, control is implicitly transferred to the instance initializer blocks and instance field initializers of the current class. Initializers are executed in the order in which they appear in the program text. Only after the initializers complete their work is control transferred to the rest of the constructor. The remaining features of the constructors relate to the Java memory model. If a class, or one of its ancestors, overrides the method finalize(), then the completion of the constructor will happen before ( happens-before ) the method runs finalize(). If any thread saw a reference to an object after the constructor completed, then it is guaranteed that this thread will see the correctly initialized final-fields of the object, the initialization of which occurred before the completion of the constructor.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION