Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2007, 04:02 PM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Constructors return void?


I have read in more than one java book that ‘all instructors return void’ but this has always puzzled me.
In a java assignment statement like:

  JPanel pan = new JPanel();

-surely the right hand side of the equals sign must evaluate to an instance of an object of the class JPanel and not ‘void’ else how can this assignment make any sense? And for the right hand side of the equals sign to evaluate to an instance of an object of the class JPanel, surely the call to the zero-argument constructor
(I.e. the <JPanel()> part) must return an instance of an object of the class JPanel? If it returned void then wouldn’t the assignment give the local variable <pan> a value of ‘void’ ? Can anyone clear up my confusion here?

                        :)Andrew Hamilton.

 
Old January 18th, 2007, 08:21 AM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello

First thing is that, Constructor does not have any return type. Even constructors don't have void as return type.
and by using "JPanel pan = new JPanel();" you are creating new objects of the class.
The "new" will return the instance of the "JPanel" class that is referenced by the "pan" variable
So the "pan" will contain the reference to the instance of "JPanel" only.

 
Old January 19th, 2007, 02:52 PM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again

I find that most eccentric. If a constructor doesn’t return any return type, then, strictly speaking, doesn’t that mean that a constructor cannot be called a ‘function’? I thought it was correct to call a constructor a function but, as I understand it, in computer programming terminology, a function must have, among other things, a single defined output type (its return type) else, by definition, it is not a ‘function’.

Until now I had always just assumed that the purpose of the keyword ‘new’ was only to remind the reader that a ‘new’ instance of an object of the class will be returned as a result of the constructor being executed. But, in our example, as “new returns an instance of the JPanel class” then does that mean that the keyword “new” represents a function call - like a call to a method that happens to return an instance of an object of the JPanel class? If so, then what would its “arguments” be? I find this whole thing most eccentric.
 
Old January 19th, 2007, 09:50 PM
Authorized User
 
Join Date: Oct 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mrafaqi Send a message via Yahoo to mrafaqi
Default

Basic thing is, Constructor don't have return type and even don't return void since these are very different than ordinary method. You should clear in mind that when see the statement like this

     JPanel pn = new JPanel();

than you'r creating an object of pn and there is not calling of an ordinary method (function) in java that return something. consider this thing same as

     int i = 15;

because, you are not even returning something here and both are actually two declare statements for different variables.




Regards

ALI_R

 
Old February 13th, 2007, 05:59 AM
Registered User
 
Join Date: Feb 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

logically constructor returs refererence to the object which is of the type 'type' itself






Similar Threads
Thread Thread Starter Forum Replies Last Post
exit from a void function Necromancer26 C# 3 October 16th, 2012 09:06 AM
error thrown by: Void AbortInternal() prv299 ASP.NET 2.0 Professional 1 June 12th, 2008 07:48 AM
Constructors madhuriguda C# 2 April 29th, 2008 08:11 AM
Cascading Constructors rodmcleay General .NET 4 May 18th, 2007 03:57 AM
Real Time Application of Void class pandian Java Basics 0 October 9th, 2006 05:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.