Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use constructor if I can initialize instance variable another way

Tags:

java

Why should I use constructor to initialize instance variables while it is possible at the point of their declaration?

class Frog {
     public int x = 4;
     Frog() { // Why should I use you }
}
like image 433
Eugene Avatar asked Dec 09 '25 22:12

Eugene


1 Answers

You should use me because I will help you keep your initializations in one place. Because it will help you other colleagues to know where to expect initializations and not miss one if they're scattered aroudn the code.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!