| Author: UltimateRengan 25 Jul 2008 | Member Level: Diamond | Rating: Points: 1 |
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
http://allu.wordpress.com/2006/11/19/difference-between-a-constructor-and-a-method/
|
| Author: chandramohan 25 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
Constructor does not have a return type. - Constructor does not have a return statement in its body. - Name of constructor is the same as that of class in which it is declared.
Code: public class ClassA {
public ClassA(){ } }
|
| Author: sunil 25 Jul 2008 | Member Level: Bronze | Rating: Points: 5 |
Constructor is a special type of memeber for a class.
it initialises the values to its fields.
when u add a class by default a constructor is added. with its class name like public default() { } Method does nt return any value. Methods do not have get and set acceessors. inorder to reduce length of ur code u can call ur method like BindGrid() Method which displays data in Gridview or any databoundsource control.
|
| Author: chandramohan 25 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
a constructor is a method in the class which gets executed when its object is created. Usually, we put the initialization code in the constructor. Writing a constructor in the class is damn simple, have a look at the following sample:
public class Test
{
public Test()
{
//Initilize code in the constructor. } }
Constructors doesn't returns any value. References and pointers cannot be used on constructors and destructors because their addresses cannot be taken. Constructors cannot be declared with the keyword virtual. If the constructors is defined as private, we can not create instance of that class.
Method:
In methods we can define variables.These variables scope is within methods only. If you declare a public variable , it will be accessed in all mehtods. Methods may/maynot contains return type. Methods could be inhereted in derived class(Note That mehtod should be Public)
|
| Author: Muralee Krishnan.K 25 Jul 2008 | Member Level: Gold | Rating: Points: 6 |
A constructor is a method in the class which gets executed when its object is created. Usually, we put the initialization code in the constructor. Writing a constructor in the class is damn simple, have a look at the following sample:
public class Test
{
public Test()
{
//Initialize code in the constructor. } }
* Constructors doesn't returns any value. * References and pointers cannot be used on constructors and destructors because their addresses cannot be taken. * Constructors cannot be declared with the keyword virtual. * If the constructors is defined as private, we can not create instance of that class.
Method:
* In methods we can define variables.These variables scope is within methods only. * If you declare a public variable , it will be accessed in all methods. * Methods may/may not contains return type. * Methods could be inherited in derived class(Note That method should be Public)
|
| Author: Lakshmankumar M 25 Jul 2008 | Member Level: Gold | Rating: Points: 6 |
a constructor is a method in the class which gets executed when its object is created. Usually, we put the initialization code in the constructor. Writing a constructor in the class is damn simple, have a look at the following sample:
public class Test
{
public Test()
{
//Initilize code in the constructor. } }
Constructors doesn't returns any value. References and pointers cannot be used on constructors and destructors because their addresses cannot be taken. Constructors cannot be declared with the keyword virtual. If the constructors is defined as private, we can not create instance of that class.
Method:
In methods we can define variables.These variables scope is within methods only. If you declare a public variable , it will be accessed in all mehtods. Methods may/maynot contains return type. Methods could be inhereted in derived class(Note That mehtod should be Public)
|
| Author: Ashok 25 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
Constructor is used to create an instance of object.
Method is used to perform some operations.
by default constructor's return type is that of a class in which it is defined so no need to write explicitly the return type. [don't know if u know c or not, if yes then it is mallock type casted to the pointer of that class type] a method must[not can...., as even if it returns nothing it should have a return type of VOID] have a return type. just as methods constructors can be public,private,static etc. but as a general guideline it is advisable to have constructors public. constructor anme should be exactly[case sensitive] same as the calss name last but not the least even if u dont define a default constructor then when you instanciate an object ...it is the system who defines one for you and calls it in order to create an instance of object...
1. constructor name should match with a class name, but for a method a name could be of anything.
2.constructor don't have any return type, where as a method can have a return type.
|
| Author: Ashok 25 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.
Constructor needs to have the same name as that of the class whereas functions need not be the same.
* There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. * There is no return statement in the body of the constructor. * The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.
Constructor is used to create an instance of object.
Method is used to perform some operations.
|
| Author: Ratheesh 31 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
constructor
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
method
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
|
| Author: asdf 14 Aug 2008 | Member Level: Silver | Rating: Points: 0 |
It’s a special type of method which is having same name of class for initialization purpose. It wont return any value. It will be called while instance of class is created.
|
| Author: Raghav 17 Aug 2008 | Member Level: Gold | Rating: Points: 3 |
Very Good Prakash John. Your definition of constructor is crystal clear. Yes it will be called when an instance of a class is created. It is called every time whenever a new instance of a class is created.
Raghav
|