mindfreak
mindfreak

Reputation: 455

How does constructor return if it doesn't have any return type?

This seems to be quite a confusing question. Per the definition, I understand that constructor is a special type of method used to initialize the state of an object and/or assign values to instance variables.

Also someone in Stack Overflow mentioned that constructor returns an object (instance) of a class, as opposed to what a normal method does/returns?

Despite going through lots of tutorials and reference materials, I couldn't find a concrete reason as to how constructor is able to return a value without the presence of a return statement.

I'm curious to find out the internal working of the entire process.

Upvotes: 1

Views: 2067

Answers (4)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

In bytecode

Test1 t1 = new Test1();

looks as follows

NEW test/Test1    //create an uninitized instance of Test1
DUP
NVOKESPECIAL test/Test1.<init> ()V  // call construcctor  
STORE 1        // save reference to created instance in local var

and this is constructor, void method in fact with special name <init>

public <init>()V   //V means no return value, void
   L0
    LINENUMBER 3 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V  // call super constructor
    RETURN

Upvotes: 1

Akash Magoon
Akash Magoon

Reputation: 899

A Java constructor does not return anything. A constructor simply initializes a new instance of an object of a specific class. Sometimes constructors will have System.out.Println("text") which may lead you to think it returns something, but you can have that statement in any method that doesn't have a return type.

Upvotes: 1

MStodd
MStodd

Reputation: 4746

It gives the JVM the 'return' opcode:

'return' returns to the calling method: http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings

Code for a default constructor:

aload_0
invokespecial   #1; //Method java/lang/Object
return

Upvotes: 2

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

Constructors don't return anything. A constructor simply initializes an instance.

A new instance creation expression

new SomeExample();

produces a reference to a new instance of the specified class

A new class instance is explicitly created when evaluation of a class instance creation expression (§15.9) causes a class to be instantiated.

and invokes the corresponding constructor to initialize the created instance

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

Upvotes: 3

Related Questions