1

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.

10
  • 2
    A constructor doesn't return anything. May 20, 2015 at 1:36
  • That's how the language is designed, the constructor "returns" an instance of the object itself, this is done automatically May 20, 2015 at 1:37
  • 1
    The constructor does not return anything, but it's also not a normal method. It only initializes the object instance. The use of the new keyword along with a constructor returns the object's reference. Think of it as the constructor setting everything up, and the word new returning the memory location of the set up object. If you ever work with a language that requires manual memory management (like C++) this concept becomes a whole lot clearer.
    – JNYRanger
    May 20, 2015 at 1:45
  • 1
    @mindfreak I didn't expect you to know C++, but the reason why this concept seems foreign to you is because Java is designed to abstract away a lot of the lower level concepts, especially those involving memory management. I highly recommend you do look into what a reference is & how they work (or if you're feeling adventurous, do some research on what pointers are in lower-level languages as they are very closely related). What you should take away from this question is that constructors don't return anything only initialize objects. However, the new "returns" a reference for assignment
    – JNYRanger
    May 20, 2015 at 12:59
  • 1
    @mindfreak Unfortunately I don't have any good recs. I learned this type of stuff by applying knowledge of lower-level languages to higher level ones (well that and university). Maybe learning the basics of memory management in a lower-level OOP language (such as C++) will help clarify how this all works. Java syntax is based on C/C++ so it won't be like looking at Chinese, but there is some additional syntax used for dealing with pointers along with allocation & deallocation of memory including the use of object deconstructors, which does exactly what you think they do =)
    – JNYRanger
    May 20, 2015 at 17:27

4 Answers 4

3

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.

2

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
1

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.

1
  • @magoon - Agreed, constructor does not return anything. Consider this, in a normal scenario one would create getter & setter methods to retrieve the value, correct ? However, in case of constructor after the initialization stuff, what is the magic that happens behind the scene that prompts the code to display the output ?
    – mindfreak
    May 20, 2015 at 2:14
1

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
3
  • @dorofeev - As opposed to what we have in getter-setter method to return the value, I wish to know the internal mechanisms that propels constructor to display output even when there is no return statement in it ? Question is more focused on "HOW ASPECT" ?
    – mindfreak
    May 20, 2015 at 2:38
  • 'new' creates an object, runs an initializer (constructor) on it and then returns a reference to created object May 20, 2015 at 3:31
  • 1
    @mindfreak The "how" concept is related to two thing: memory management & the generated bytecode. As Evgeniy shows in his answer the bytecode generated does in fact have a return statement to release control back to the caller. However, it's not actually returning the newly created object itself, but instead a memory reference to that object. To go much deeper than this might go out of scope for SO.
    – JNYRanger
    May 20, 2015 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.