Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By quad64bit
  • 1 Post By DarrylBurke

Thread: Constructor

  1. #1
    sri246 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Post Constructor

    Hi,can any one help me for,
    Constructor returns a value,But how it will be.

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Constructor

    Can you please rephrase your question?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Constructor

    I think what you're asking is what the return type is for a constructor, and the answer is that a constructor returns an object of whatever type the class is. For example:

    Java Code:
    public class Car{
    
    public Car(Color color, String model){
    ...
    ...
    }
    
    }
    When you create an instance of a Car (as in the below example), the constructor for the Car class is executed, and a Car object is returned, and in this case
    we assign that returned value to the myNewCar variable:

    Java Code:
    Car myNewCar = new Car(Color.Blue, "Honda");

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Constructor

    answer is that a constructor returns an object of whatever type the class is
    It is true that the result of a invoking a constructor with the key word 'new' results in an instance of the class the constructor resides in, but saying that a constructor itself has a return type is confusing. The constructor explicitly does not have a return type, and requires the 'new' keyword to do anything at all.
    DarrylBurke likes this.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Constructor

    My $0.02

    An object is not returned at all. Only a reference but certainly not by the constructor.

  6. #6
    sri246 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Constructor

    Hi,
    can any one help me,
    how constructor returns a value.

  7. #7
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Constructor

    A Constructor does not return a value! Constructors are only used when creating instances of a class. See Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects) .

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Constructor

    sri246, please go through the Forum Rules -- particularly the second paragraph.

    I've merged the two threads. You already had good answers, so why did you stat a second thread with the same question?

    db
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 10:50 AM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 10:17 AM
  3. Replies: 5
    Last Post: 12-14-2011, 02:47 PM
  4. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •