Results 1 to 2 of 2
- 08-01-2012, 09:06 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 6
- Rep Power
- 0
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?
}
- 08-01-2012, 09:17 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
How to call a master constructor from a minor constructor?
By b.m in forum New To JavaReplies: 5Last Post: 12-14-2011, 01:47 PM -
Method and Constructor
By swikar.java in forum New To JavaReplies: 7Last Post: 10-21-2008, 02:41 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Java constructor method
By carl in forum New To JavaReplies: 2Last Post: 07-31-2007, 02:15 PM
Bookmarks