This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Information about Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Information about Constructor" Watch "Information about Constructor" New topic
Author

Information about Constructor

Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Hi Java Experts,

I read that "Constructors do not have a return type—not even void". BUT does it mean that constructors does not return any thing?
I believe that when a new instance is created the constructor call returns the handle/reference of that object. Is this true?

Please clarify on this key point.

Thanks,
Faraz
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Faraz Alig wrote:
I read that "Constructors do not have a return type—not even void". BUT does it mean that constructors does not return any thing?

simply you can put *return;*
Faraz Alig wrote:
I believe that when a new instance is created the constructor call returns the handle/reference of that object. Is this true?

Constructors logically return an object, but that is take care by JVM.
Kevan Ryan
Greenhorn

Joined: Feb 08, 2010
Posts: 9
Faraz,

Basically, constructor is used to initialize instance variables within a class. No new instance variables are created. Rather, variables defined within the class can be set to a default value, such as 0 or 0.0 or something. No arguments are needed, and no 'return' statement is needed.

-Kevan

||
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39097
    
  23
It might be more precise to say that constructors without arguments are permitted. If there are fields to be initialised, then arguments may be necessary to provide their values.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14196
    
  20

Seetharaman Venkatasamy wrote:Constructors logically return an object, but that is take care by JVM.

I don't think you should see it like that.

Constructors don't return anything. Constructors look like methods, but they are not really methods. A constructor is a special block of code that is called to initialise a new object. The constructor does not return the object, it just initialises the object that the JVM has created.
 
GeeCON Prague 2014
 
subject: Information about Constructor