Results 1 to 5 of 5
  1. #1
    swikar.java is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default Method & Constructor

    Hello,

    Can anybody tell me difference between method and constructor?
    Also what mean of use this in method and costructor?

    Thanks,
    Swikar

  2. #2
    swikar.java is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    Hello, how r u?

  3. #3
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    A method is a way of grouping together a series of statements. It is also known as a function, and it may take values (inputs) and return them (outputs).

    For example, this function adds the two numbers (a and b) and returns them:

    Java Code:
    public int add(int a, int b) {
      return a+b;
    }
    A constructor is like a method, but it has a special purpose: to create an object. For example, suppose you have a Pupil class, and each pupil has a name (which is a string):

    Java Code:
    public Pupil(String name) {
      this.name = name;
    }
    In this example, the Pupil object is created and its name is set to the value that was input.

    There are some key differences between a method and a constructor:
    • A constructor creates a new object, an instance of the class to which is belongs to. A method might not necessarily create any objects.
    • You must specify the type of object returned by a method (which is int, in my example above), even if this is void (which means return nothing). My example function 'add' returns an int. You cannot specify a return value for a constructor - since, they don't return a value.
    • A method can have any name (within certain limitations). A constructor must have the same name as its class. Hence, my Pupil class has a constructor called Pupil!


    For more information, see ccnyddm.com/AdvJava/java_constructor_tutorial.htm (I have taken the www off so I can post the link).
    Last edited by Paul Richards; 10-21-2008 at 02:23 PM.

  4. #4
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Can we keep this to one thread please! Continued here: Method and Constructor

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Method and Constructor
    By swikar.java in forum New To Java
    Replies: 7
    Last Post: 10-21-2008, 03:41 PM
  2. Replies: 0
    Last Post: 04-17-2008, 08:43 PM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Differences between constructor and method
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-19-2007, 09:53 AM
  5. Java constructor method
    By carl in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03: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
  •