+ 1
Constructor and methods
Between constructor and method how do I know which to use in a java program...
4 Respostas
+ 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).
+ 2
I'll add... Constructor dont return any type or data. And must have same name as a class
+ 1
I know all that but how do I know when to use a constructor or a method
+ 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