+ 1

Constructor and methods

Between constructor and method how do I know which to use in a java program...

23rd Oct 2017, 8:09 PM
Challiebenz
Challiebenz - avatar
4 Answers
+ 5
A method is a function that is a member of a class. Constructor is a method that is called automatically when an object of the class is created. You should use constructors to initialize the object, and regular methods to perform any other tasks on the object. PS: Please, state the language you're asking about in your question (add a tag).
23rd Oct 2017, 8:17 PM
deFault
+ 2
I'll add... Constructor dont return any type or data. And must have same name as a class
23rd Oct 2017, 9:51 PM
Alexander Strebchuk
Alexander Strebchuk - avatar
+ 1
I know all that but how do I know when to use a constructor or a method
23rd Oct 2017, 11:41 PM
Challiebenz
Challiebenz - avatar
+ 1
Reason you create constructor is to inform the world about dependencies, a class needs to do its job. Anyone by looking at your constructors should be able to figure out, what he needs in order to use this class.Ā  Method is a piece of code that couldĀ perform someĀ operations,Ā then return something back to whatever happened to ā€œcallā€ the method. Or, aĀ method will perform some operations, without needing toĀ return anything when itā€™s done
24th Oct 2017, 4:54 AM
Š ŃƒŃŠ»Š°Š½ Š„усŠ°ŠøŠ½Š¾Š²
Š ŃƒŃŠ»Š°Š½ Š„усŠ°ŠøŠ½Š¾Š² - avatar