SyntaxFix
Write A Post
Hire A Developer
Questions
🔍
[java] Initialize class fields in constructor or at declaration?
Home
Question
Initialize class fields in constructor or at declaration?
My rules:
Don't initialize with the default values in declaration (
null
,
false
,
0
,
0.0
…).
Prefer initialization in declaration if you don't have a constructor parameter that changes the value of the field.
If the value of the field changes because of a constructor parameter put the initialization in the constructors.
Be consistent in your practice (the most important rule).
Examples related to
java
•
Under what circumstances can I call findViewById with an Options Menu / Action Bar item?
•
How much should a function trust another function
•
How to implement a simple scenario the OO way
•
Two constructors
•
How do I get some variable from another class in Java?
•
this in equals method
•
How to split a string in two and store it in a field
•
How to do perspective fixing?
•
String index out of range: 4
•
My eclipse won't open, i download the bundle pack it keeps saying error log