Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
Like Tree3Likes

Thread: Object and Class

  1. #21
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Object and Class

    Hi,

    the constructor does not return a value because it is only responsible for the initialization. So the new command has to
    - allocate the memory
    - call the constructor
    - return the reference

  2. #22
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Object and Class

    Quote Originally Posted by jmohandos304 View Post
    I think I asked the following question but when I searched for it I could not find that thread. Hence I am posting the question here:

    Suppose I take the line:

    Java Code:
    A a = new A();
    We all know that constructor does not have a return value. Then how does the object "a" get initialized?
    That's what the JVM does for you. It creates the object using the constructor, then assigns it to the variable you specify ("a" in this case).

    You must resist the urge to see the constructor being some kind of special method. It is not a method. It's a constructor.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Object and Class

    Why are you asking that again here? You asked it in this forum and received lots of responses.
    see --> Basic Question

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #24
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Object and Class

    You must not mistake the constructor for a method. Even though they look similar, they are two different things. (Don't get into a habit of calling them a "special method". Any professor who calls them that should be fired).

    Methods operate on an object (or sometimes a class: static) and possibly have return values. Constructors are ways to create an object. In your example, the JVM finds the constructor with no arguments, and that initializes the object. The JVM also knows that the reference to the object that was just created should be assigned to variable "a".

    That's all. No methods. No confusion. World Peace.... Profit.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #25
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Object and Class

    Goddamit, why do I reply to the last post of page #1. Christ....
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #26
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Object and Class

    Quote Originally Posted by SurfMan View Post
    Christ....
    Yes, my son?

    Seriously: its Friday dude. The day of the week where the two halves of your brain communicate through paper airplanes only.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #27
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Object and Class

    Quote Originally Posted by gimbal2 View Post
    Yes, my son?

    Seriously: its Friday dude. The day of the week where the two halves of your brain communicate through paper airplanes only.
    I wish they were communicating _at all_....

    *hits the espresso machine*
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 05:37 AM
  2. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  3. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  4. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

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