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

What is a heavyweight component?

544


How is string immutable in java?

545


What is natural ordering in java?

536


What is the difference between double and float variables in java?

638


What are constants?

603






How to compare strings in java?

508


Can we have try without catch block?

647


What are the important features of Java 10 release?

513


What is the use of System class?

614


Can we declare a class as static?

598


What is the main advantage of passing argument by reference?

507


What does arrays sort do in java?

545


Explain OOPs concept.

653


Difference between stack and queue?

595


How can you read content from file in java?

601