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
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");
0
😂 constrictor man my question is if we can print simply so why we are using constrictor
0
yes yes I ask this restoring