Results 1 to 2 of 2
  1. #1
    brocksoffice is offline Member
    Join Date
    Jul 2012
    Rep Power

    Default Java - Constructor Method versus Constructor

    Is there any difference between the constructor within a class that defines an object and the constructor method called using the new operator... or are they the same?

    for example

    Class ClassName
    public constructor(parameters)<-- Is this called a constructor?

    ClassName cn = new ClassName(); <-- Is this called a constructor method?


  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Java - Constructor Method versus Constructor

    A constructor looks like a (non-static) method that has the same name as the class and no return type. If you construct a new object (with the 'new' opertor), the new operator allocates just enough memory on the heap and sets all bits of that memory to zero; next it calls your constructor so it can properly initialize all memory and do what it has to do. The memory is implicitly pointed to by the 'this' reference.
    So, if you rename the thing 'constructor' in your code example to 'ClassName' you have the definition of a constructor for the ClassName class.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  2. Method and Constructor
    By in forum New To Java
    Replies: 7
    Last Post: 10-21-2008, 02:41 PM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Java constructor method
    By carl in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 02:15 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