I wanted my class to only have one object created at any given time? | Sololearn: Learn to code for FREE!
Nowy kurs! Każdy programista powinien nauczyć się Generative AI!
Wypróbuj darmową lekcję
+ 1

I wanted my class to only have one object created at any given time?

I want to limit the amount of object which can be created from a particular class , It should only have one or zero object at any give moment , I have tried a way and it seems to work Please check it out and tell me some improvements I maybe will be able to make https://code.sololearn.com/cUzUiPEKEBPL/?ref=app

29th Sep 2019, 3:20 PM
Shahil Ahmed
Shahil Ahmed - avatar
1 Odpowiedź
0
That is fine, but you can make it easier. Just type in the constructor some int variable equal to- 0 that everytime time you create object from this class he will be raise by 1, then, put a if statement in the same constructor that if the int variable is higher then 1 you will call the distractor.
29th Sep 2019, 3:27 PM
KfirWe
KfirWe - avatar