• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Information about Constructor

 
Faraz Alig
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic