Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 13-11-2009
Member
 
Join Date: May 2008
Posts: 69
use of methods and constructor

I am learning Java programming, but I am quit confused about constructor and method. What is the use and basic difference between both of them?
Reply With Quote
  #2  
Old 13-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: use of methods and constructor

Constructor:
A constructor is a member function of a class which is used to create objects of that particular class. Constructor has same name as the class.Constructor don't has any return type, and it is invoked by using new operator.

Method:
A method is an ordinary member function of that class in which it is defined. It has its own name, a return type (which may be void), and is invoked using the dot operator.

e.g.
class PQR{

PQR(){ // PQR is a constructor
------
}
PQR( String x){ //PQR Constructor has one argument
-------
}
}

A method has its own name and return type. It is invoked using the dot operator.
Reply With Quote
  #3  
Old 13-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: use of methods and constructor

Below are the few differences between methods and constructors:


1. A member function of a class is called as constructor.It is used while creating objects of that class. constructor has the same name as the class , with no return type, and It is called using the new operator.

2. Ordinary member function of a class is called method. It has a return type (this can be may be void).

3. In OOPs, a constructor in a class is a special block of statements invoked at the same time of an object is created, otherwise when it is declared .

4. Object responds and receives to a particular type of message through methods. In C++ language a method is of member function
Reply With Quote
  #4  
Old 13-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: use of methods and constructor

Constructor is a member function of the class,and the name of constructor & name of class is same.
see the below example constructor:

class student{

student(){ //name of Constructor
------
}
student( int a){ //Constructor with integer type of argument
-------
}
}

On the other hands a method is an ordinary member function of a class.
Reply With Quote
  #5  
Old 14-11-2009
Member
 
Join Date: Feb 2009
Posts: 96
Re: use of methods and constructor

here is both a constructor and a method

class dog{

public dog{} //constructor makes an object of the class dog

public void setFeatures{
string face;
string color;
int legs;
} // this is a method-- basically it adds items to the object
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "use of methods and constructor"
Thread Thread Starter Forum Replies Last Post
What is constructor overloading in c++ Mast Maula Software Development 4 08-01-2011 10:34 AM
What is the use of private constructor in C++? Juaquine Software Development 5 05-03-2010 04:41 PM
copy constructor Vs constructor Truster Software Development 5 08-02-2010 02:49 PM
Selecting a constructor to use with new in C++ Carlton Software Development 5 02-02-2010 12:30 AM
Constructor in Java Dharamsi Software Development 4 06-03-2009 12:47 PM


All times are GMT +5.5. The time now is 07:58 AM.