What is the difference between constructor and method in java?
Constructor invoke automatically when u create a object of that class bt for a method u need to invoke or call them to excute
And syntactically difference is ..
Constructor name should be same as class name and method should have different name apart from class name...
Constructor doesn't return any value bt method can...
constructor name should be same as class name whereas method name can be any
1.Constructors are without return type.1.methods are with return type
2. Constructors memory is allocated on creation of object. 2. Memory is allocated when method is called
3.constructors name are same as class name. 3.method name can be anything....
constructors are needed to create object instances of classes..if we dont create a constructor, a default will automatically be created by the compiler with no arguments..constructor has the same name as class name and are automatically called when we create instance of a class..now methods will have to be created explicitly, if we dont specify any method; ofcourse nothing is implictly created..and also unlike constructor, methods are needed to be called explicitly..
Constror is call automatically then object created but method is call by particular object
Constructor have no return type while method have also return type.
Constructor means variable initialized and method means private variable another class to calling in variable public method.
A method having same name as class.
Only because of constructor we can create instance or object of class ...method does not participate to instantiation of class..
A method has to have a return type but a constructor does not have a return type.