Does the Java constructor return an object reference?

I know that Java constructors cannot be of any type, and it is interesting that it cannot even be void . A logical explanation for this would be that the constructor returns an initialized reference to the object.

 MyClass myObject = new MyClass(); 

The constructor myClass will now return a reference to the object after its creation and save it in the variable of the MyObject object and that the constructor cannot have a return type.

It is right? Can anyone confirm this?

+5
source share
4 answers

No, in fact, the constructors compiled into the class file similar methods that have the name <init> and the void type of the return value. You can see these "<init>" calls in stacks. The expression new Type() compiled as a new statement that just creates an instance of Type and an additional invokation ( invokespecial ) method to one of the constructors declared in Type .

The verifier ensures that such a special method is called exactly once on the newly created instance and that it is called before any other use of the object.

Its just a solution for designing a programming language so that designers do not have a return type in terms of the Java language. After all, new Type(…) is an expression that evaluates to the newly created instance of Type , and you cannot get the return value from the constructor using this programming language construct. Also, if you add a return type, Java will unconditionally assume that it is a method, even if it has the same name as the class.

This is just how it was defined : (This simplifies the analysis of the class definition)

SimpleTypeName in ConstructorDeclarator must be the simple name of the class containing the constructor declaration, or a compile-time error occurs.

In all other respects, a constructor declaration looks like a method declaration that has no result (§8.4.5).

+6
source

I suppose you could say that constructors have a “special syntax” used specifically to return instances of the desired object. You do not indicate the type of return in these cases. The new keyword is used with the constructor method to instantiate the class type.

If you want to control the type of the returned instance generation method, then you should probably look at a factory-type template in which the static method creates an instance (using the constructor) and then returns a more explicit type (for example, super type or interface type).

This pattern is good if you want to decide which type will be returned based on some parameter, but leave the actual type hidden to the consumer of the instance generation method.

+1
source

The constructor is not methed. It does not return anything. It is used for initialization purposes, it is especially useful when these initializations depend on the parameters or it is possible that exceptions will be thrown (although both of them are optional).

So, unlike a method, it is not inherited and does not have a return type (not even empty).

0
source

The idea is that you "create" an instance of MyClass by calling the constructor itself. The idea of ​​the constructor is to instantiate and not return. By creating the myObject, you can reference public methods and variables, part of your declaration, which will provide you with the required data returned as an answer to the call. It is important to understand that the constructor does not return anything, it simply creates an instance, which can then be used to refer to methods and variables (which return data) declared in the instance class.

0
source

Source: https://habr.com/ru/post/1205760/


All Articles