What is the difference between method and constructor ?

Answer Posted / dineshkumar

A method is a set of code that will perform a specific task. It must have a name which is accurate to the task it performs, must have a return type(if nothing must be prefixed with void), an optional access specifier, may have parameters/arguments. It is called whenever it is needed.A method can be overloaded and overridden.

A constructor is a special block of code which has the same name as its class name with no return type. It is executed only once for an instance of the class. It is, logically, used for initializing the object(its parameters). A constructor cannot be overridden but can be overloaded by differing no.of arguments.

Is This Answer Correct ?    12 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the concept of hashtables?

611


What is a variable declaration?

517


What is the use of math abs in java?

547


What is outofmemoryerror in java?

561


Explain jvm, jre, and jdk?

557






What does || mean in vectors?

475


Why runnable interface is used in java?

564


What are the two ways in which thread can be created?

596


Is upper case in java?

525


What is return code?

558


How do you sort a string in java?

535


What is the console in java?

674


What is constant in programming?

554


Is java written in c?

538


Explain when classnotfoundexception will be raised ?

563