+ 2
What's the difference between Constructors and Methods?? With example please.
8 Answers
+ 4
The main difference is constructors are used by classes when an object is instantiated. Methods are some functions in a class to get/set values and do other works
+ 4
HonFu Thanks Very much, because am beginning to understand.
+ 3
A constructor creates an instance of a class.
A method can be used by an existing instance.
+ 3
Constructor is internally a nonstatic method with name <init> and voidreturn type. It does not return anything. Internally first object is allocated and then its constructor is called. Object is not allocated with constructor itself.
In other words the syntax new Object()not only calls the constructor but also creates new object and after calling the constructor returns it. The Suns' Java tutorial stands that "The new operator is followed by a call to a constructor, which initializes the new object." Initialize does not mean create.
+ 2
The constructor gets called, everytime you create an instance of your class.
Methods are called manually.
See the code example:
https://code.sololearn.com/cyOxLhWDvX82/?ref=app
+ 2
ALI EL-ALAOUI I don't understand this Language can you pls translate it for me?
+ 2
Mr. Mus, 'constructor has no return type, has the same name as the class and serves to initialize the attributes; a method is a function tied to an object.'
0
La diffrence c'est que un constructeur ne possede pas le type de retour et possede le mĂȘme nom que celle de la classe, en plus permettant d' intialliser les attributs de la classe, mais une mĂ©thode c' est une fonction associe Ă un objet ;
voilĂ la taraduction en englais:Mr.Mus ,The difference is that a constructor does not have the return type and has the same name as the class, in addition to allowing to classify the attributes of the class, but a method is a function associated with an object. ;