What is the return type of constructor in java?

As we know, we do not need to add any type of return value to the Java constructor.

class Sample{ ..... Sample(){ ........ } } 

In Objective-C, if we create a constructor, it returns a pointer to its class. But I do not think this is not necessary.

 AClass *anObject = [[AClass alloc] init];//init is the constructor with return type a pointer to AClass 

Similarly, the constructor is converted to a method that returns a reference to its own class?

Like this:

 class Sample{ ..... Sample Sample(){ ........ return this; } } 

Does the compiler make the return type a reference to the same class as the constructor? What happens to the constructor? Any link to learn this?

EDIT:

In fact, I want the answers to be at the bytecode level or the JVM level or even lower.

+12
java constructor jvm bytecode
Jan 15 '12 at 7:08
source share
7 answers

Many answered how constructors are defined in Java.

At the JVM level, static initializers and constructors are methods that return void. Static initializers are static methods, however, constructors use this and nothing needs to be returned. This is because the caller is responsible for creating the object (not the constructor)

If you try to create an object in byte code without calling the constructor, you will get VerifyError. However, in the oracle JVM, you can use Unsafe.allocateInstance () to create an object without calling the constructor.

The static initializer is called <cinit> , which takes no arguments and the constructor is called <init> . Both have a void return type.

For the most part, this is hidden from the Java developer (unless they generate bytecode), however the only time you see these "methods" in the stack trace (although you cannot see the return type)

+16
Jan 15 '12 at 9:12
source share

While constructors are similar to methods, they are not methods. They do not have a return type, are not inherited, and cannot be hidden or overridden by subclasses.

Constructors are called by class instance creation expressions (mainly using new ), explicitly called from other constructors (using the syntax this(...) or super(...) ) and the string concatenation operator. There is no other way to call the constructor (in particular, they cannot be called, like other methods).

See section 8.8 Java Language Specifications for more details.

+3
Jan 15 '12 at 7:21
source share

Is the constructor converted to a method that returns a reference to its own class?

No , but yes, if indicated for this.

Does the compiler add a return type reference to the same class as the constructor ??

No it's not

What happens to the constructor?

This is the method that runs when the object is created. Typically using the β€œnew” keyword. It can perform some preliminary task or return something or assign some values ​​during construction.

Any link to learn this.

+3
Jan 15 '12 at 7:24
source share

Constructors are similar to methods, except that they use the class name and do not have a return type. The whole purpose of using constructors is to create an object (an instance of the class) and allocate it (via the new keyword) in memory (a bunch), and also initialize any fields, if available.

+2
Jan 15 '12 at 7:17
source share

Constructors are called through the special keyword java new , which creates (and initializes) an object of the specified specific type.

I suppose you could say the combination new , and the selected constructor "returns" an object, which in java, of course, is a pointer under the covers

+1
Jan 15 '12 at 7:21
source share

The constructor returns a class reference for the class for which it is called. For example.

 class A { int x; A(int a) { x = a; } } class B { public static void main(String asd[]) { A a = new A(4); System.out.println(a); } } 

Here, after calling constructor A(...) this constructor will return a reference to the type of class A to the caller (i.e., A a = new A(4) ).

0
Feb 17 2018-12-17T00:
source share

The constructor is used only to initialize a class member and

  • The constructor name and class name are the same
  • Constructor cannot have return type
  • The constructor is always called when creating an object.
  • The constructor is always open
-one
Mar 22 '15 at 2:53
source share



All Articles