0

I can use System.out.println("h") and we aslo can print using constructor so why we use conatucture

what is actually use where is constrictor is use

26th Sep 2017, 1:40 PM
prashant vyas
prashant vyas - avatar
4 Antworten
+ 5
Actual question (I think) : class someClass{ someClass(){ System.out.println("h"); } } 'Why print h in the constructor, when we can simply print h in the main method?' Reason: The constuctor is called when the Object is instantiated (created). So, if we always want to print something when an Object is created, it only makes sense to print it in the constructor. If we didn't, we would have to print in a different class right after creating the Object (every time!). This is not very maintainable since we may not know the reason why we are printing. It can also add more unnecessary lines of code in our program. Comparing the code: new myObject(); new myObject(); new myObject(); Compared to: new myObject(); System.out.println("h"); new myObject(); System.out.println("h"); new myObject(); System.out.println("h");
26th Sep 2017, 3:39 PM
Rrestoring faith
Rrestoring faith - avatar
0
😂 constrictor man my question is if we can print simply so why we are using constrictor
26th Sep 2017, 2:57 PM
prashant vyas
prashant vyas - avatar
0
yes yes I ask this restoring
26th Sep 2017, 4:03 PM
prashant vyas
prashant vyas - avatar