Give real example of constructor .... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Give real example of constructor ....

its use , the way it's being used

22nd Dec 2017, 11:10 AM
Samudra Gupta
Samudra Gupta - avatar
2 Answers
+ 2
class Example { private int a= 1; public void doStuff() {a+=1;} } public class MyApp { public static void main (string[] args){ Example ex = new Example();//here are the only way constructor should be used ex.doStuff();}} new keyword create object constructor call Example() ; initialize it thats it. If you trying to do something in constructor apart from it, you are doing it wrong. Each method should do only on thing, constructor should "construct" object.
22nd Dec 2017, 12:39 PM
Dima Makieiev
Dima Makieiev - avatar
0
thanks for ur reply , now my question is what's the differences between method & contractor??? still stuck why it is used😢
23rd Dec 2017, 5:51 PM
Samudra Gupta
Samudra Gupta - avatar