What's the difference between Constructors and Methods?? With example please. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

What's the difference between Constructors and Methods?? With example please.

12th Jan 2019, 10:50 AM
MusabYK
MusabYK - avatar
8 Réponses
+ 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
12th Jan 2019, 11:14 AM
Seniru
Seniru - avatar
+ 4
HonFu Thanks Very much, because am beginning to understand.
20th Jan 2019, 9:02 AM
MusabYK
MusabYK - avatar
+ 3
A constructor creates an instance of a class. A method can be used by an existing instance.
12th Jan 2019, 11:15 AM
HonFu
HonFu - avatar
+ 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.
12th Jan 2019, 12:25 PM
D_Stark
D_Stark - avatar
+ 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
12th Jan 2019, 11:24 AM
Manuel Probst
+ 2
ALI EL-ALAOUI I don't understand this Language can you pls translate it for me?
20th Jan 2019, 8:51 AM
MusabYK
MusabYK - avatar
+ 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.'
20th Jan 2019, 8:58 AM
HonFu
HonFu - avatar
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. ;
19th Jan 2019, 9:52 PM
ALI EL-ALAOUI
ALI EL-ALAOUI - avatar