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

    Default Method and Constructor

    What major difference between method and constructor in java?
    also what mean of use this in method and constructor?

    Thanks,
    Swikar

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

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

    Default

    Quote Originally Posted by Eranga View Post
    Constructor has the same name as the class name, but the method is not.
    Really?

    public class Foo {

    public Foo() {
    // a constructor
    }

    public void Foo() {
    // a method with the same name as the class - not recommended, but perfectly valid
    }
    }

    The difference is in intended usage, and the effect it has. A constructor is called to create an instance of a class, a method is called to perform some action. Every time you call a constructor (using the 'new' keyword, typically) a new instance is created. Not so with a method. We call constructors, as said, using the 'new' keyword, but call methods against either a reference to an object, or a class name, depending on whether it's a static method or not

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

    Default

    Quote Originally Posted by georgemc View Post
    Really?

    Actually that's the major thing, in the sense of what seen directly. Return type is not there too.

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

    Default

    Quote Originally Posted by Eranga View Post

    Actually that's the major thing, in the sense of what seen directly.
    Despite not actually being true??

    No offence, but I think your original answer was superficial and misleading. The usage of contructors and methods vary enormously, what they're referred to as in source code is trivial. I can see someone reading your answer and saying "oh, so apart from them having different names, there's no difference between a method and a constructor", putting a load of unsuitable code in a constructor that belongs in a method, and creating loads of new instances because they call the constructor in order to run that code. Then they can't work out why their software doesn't work.

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

    Default

    Actually I'm not talking about he usages of constructors, just want to tell how to separate those things.

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

    Default

    Errrrr, ok

    *shrug*

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

Similar Threads

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