Differentiate between the constructors and methods in Java?

DWQA QuestionsCategory: JavaDifferentiate between the constructors and methods in Java?
Editor Staff asked 1 month ago

Differentiate between the constructors and methods in Java?

1 Answers
Editor Staff answered 1 month ago

Methods Constructors
1. Used to represent the behavior of an object 1. Used to initialize the state of an object
2. Must have a return type 2. Do not have any return type
3. Needs to be invoked explicitly 3. Is invoked implicitly
4. No default method is provided by the compiler 4. A default constructor is provided by the compiler if the class has none
5. Method name may or may not be same as class name 5. Constructor name must always be the same as the class name
In case you are facing any challenges with these Java interview questions, please comment on your problems in the section below.