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
What is a heavyweight component?
How is string immutable in java?
What is natural ordering in java?
What is the difference between double and float variables in java?
What are constants?
How to compare strings in java?
Can we have try without catch block?
What are the important features of Java 10 release?
What is the use of System class?
Can we declare a class as static?
What is the main advantage of passing argument by reference?
What does arrays sort do in java?
Explain OOPs concept.
Difference between stack and queue?
How can you read content from file in java?