Javatpoint Logo

does constructor return any value

By: vinaya*** On: Tue Aug 06 14:40:30 EDT 2013     Question Reputation-5 Answer Reputation0 Quiz Belt Series Points0  -5Blank User
i did not understand this concept sir , can please explain me with exampleUp0Down

 
The default return type of a constructor is the object of the same class. You cannot make a constructor explicitly return any other data type.

class Test
{
Test()
{
return ;
}
}
In that case, compiler will not show any error.

Image Created0Down

By: [email protected] On: Wed Aug 07 02:05:25 EDT 2013 Question Reputation0 Answer Reputation392 Belt Series Points0 392User Image
Are You Satisfied :9Yes3No
 
Constructors cannot return a value; they return the constructed object.

You get an error because the compiler is looking for a constructor that takes a string as its argument. Since you did not declare a constructor the only constructor available is the default constructor that does not take any argument.

Why do I say you did not declare a constructor? Because as soon as you declare a return value/type for your method it is not a constructor anymore but a regular method.
Image Created0Down

By: [email protected] On: Wed Aug 07 02:26:04 EDT 2013 Question Reputation0 Answer Reputation153 Belt Series Points0 153User Image
Are You Satisfied :2Yes0No