Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 9 years ago

Differences bw Constructor, Method and Function in Java?

I've learnt Java for 1 week. I learnt all the stuff about looping, conditional, GUI...etc. But at the very beginning, I don't really understand the differences in function of Constructor, Method and Function and their usage in Java. Please help me by explaining briefly. Thank you!

2 Answers

Relevance
  • Anonymous
    9 years ago
    Favorite Answer

    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.

  • 9 years ago

    constructor, method all are functions.

    Constructor will be invoked automatically whenever an object of the class is instantiated. Usually it is used do some initialization, resource acquiring etc.

    A method rather member method has to be invoked with the explicitly through dot operator. If A is an object xyz is method, then A.xyz() invokes xyz with object A

Still have questions? Get your answers by asking now.