Results 1 to 5 of 5

Thread: use of methods and constructor

  1. #1
    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?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    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

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    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.

  5. #5
    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

Similar Threads

  1. What is constructor overloading in c++
    By Mast Maula in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:34 AM
  2. What is the use of private constructor in C++?
    By Juaquine in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 04:41 PM
  3. copy constructor Vs constructor
    By Truster in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:49 PM
  4. Selecting a constructor to use with new in C++
    By Carlton in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 12:30 AM
  5. Constructor in Java
    By Dharamsi in forum Software Development
    Replies: 4
    Last Post: 06-03-2009, 12:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,958,025.53147 seconds with 17 queries