0

I read in many java books that constructor have no returns type , it means that it does not return anything ? is it really happen ? or it may return something ? I want to know the reason.

Please anyone give me the technical reason.

Jesper
  • 202,709
  • 46
  • 318
  • 350
snehal
  • 1,798
  • 4
  • 17
  • 24
  • It **do not** return anything. – lulyon Sep 09 '13 at 11:29
  • [Related SO question](http://stackoverflow.com/questions/14509968/what-is-constructor-in-java-if-it-is-not-a-member-of-class). – rocketboy Sep 09 '13 at 11:30
  • 2
    In theory, the constructor does not return anything but simply modifies the object that will be returned by the `new` operation. In practice, it wouldn't work for you to return anything other than `this` anyway, so the return value would be redundant. – Hot Licks Sep 09 '13 at 11:31

8 Answers8

2

I read in many java books that constructor have no returns type , it means that it does not return anything ?

Yes, its return type is V for void.

is it really happen ?

Yes.

or it may return something ?

No.

I want to know the reason.

The question shouldn't be; Why not?,

The question should be; Do I really have? and the answer is no.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

it means that it does not return anything ?

No,It won't.

is it really happen ?

Yes It happens

or it may return something ?

Again No,it won't return anything.

Providing constructor is optional,the name it self indicating, it just helps in construction of the object.Wont return anything.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Thanks for answering, and I also know that...constructor don't have any return type...so it is not returning anything but I read somewhere that Constructor implicitly returns class type itself. So, is it a true statement. – snehal Sep 11 '13 at 16:38
0

Constructors have one purpose is to create an instance of a class.Where as The purpose of methods, by contrast, is much more general. A method's basic function is to execute Java code.So Constructor never returns anything where as method may or may not return somthing.

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
0

While constructors technically return nothing, the new call creates a new skeleton object at defaults, and then calls the correct constructor(and initializer blocks).

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

Implicitly it will return the object of the corresponding class. WE should not mention the return type for constructor

upog
  • 4,965
  • 8
  • 42
  • 81
0

A constructor is a special block of code that is called whenever a new object is created. It is called to initialize the new object.

A constructor does not return anything, and you never call a constructor directly. You use the new operator to create a new object. The new operator reserves memory for the object, then calls the constructor to initialize it, and then returns a reference to the new object.

Jesper
  • 202,709
  • 46
  • 318
  • 350
0

Constructors are for initialising objects. They do their work, and that's it - the object is initialised. Returning stuff is NOT what constructors are for.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

Please refer to (Can constructor return a null object?)

It is a common behavior across all OOP languages that constructors do not have a erturn type in their signature. In fact, an instance of the class type enclosing the constructor definition is returned.

Community
  • 1
  • 1
Trey Jonn
  • 350
  • 3
  • 17