Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Constructors. What is their purpose?

  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Constructors. What is their purpose?

    I know what a constructor does. It initializes a variable. However, what does that mean? What does it mean to initialize a variable? What is the purpose of doing that?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Constructors. What is their purpose?

    No, it initializes an object, not a variable, and that's a big difference. It allocates memory for the object and all of its components, and any other housekeeping that needs be done including setting up any virtual method tables.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Constructors. What is their purpose?

    Quote Originally Posted by Fubarable View Post
    No, it initializes an object, not a variable, and that's a big difference. It allocates memory for the object and all of its components, and any other housekeeping that needs be done including setting up any virtual method tables.
    Small nitpick: a constructor doesn't allocate anything for the object to be itself; it's the new operator that does the dirty work, i.e. it allocates enough memory for the object; zeros all bits in the memory and plugs in a pointer to to the class function table; only then is the constructor called that further initializes the thing.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. interface purpose?
    By frejon26 in forum New To Java
    Replies: 5
    Last Post: 06-27-2014, 09:41 AM
  2. What is the purpose of a HashMap?
    By Tombomb in forum New To Java
    Replies: 4
    Last Post: 01-17-2012, 11:04 AM
  3. Purpose of close() ?
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 01-13-2012, 09:57 AM
  4. general purpose calculator
    By j3sr2010 in forum NetBeans
    Replies: 3
    Last Post: 06-27-2011, 07:19 PM
  5. Purpose of abstract class
    By scott in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-26-2010, 04:21 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
  •