I know that private instance variables are accessed through their public getters and setters method.
But when I generate constructors with the help of IDE, it initializes instance variables directly instead of initializing them through their setter methods.
Q1. So should I change the IDE generated code for constructors to initialize those instance variables through their setter methods.
Q2. If yes, then why IDE don't generate constructors code in that way?
============================= EDITED =======================================
I use Eclipse and Netbeans IDE
It's a general question. But as asked by @Lords would the answer depends on whether our constructor is public or protected or package private or private?
You should decide which fields you will initialise with the constructor and which to initalise with a setter. (both is possible) I prefer to use the constructor only as much as possible and often have no setters.
This should be configurable/selectable in IDE. Without knowing your IDE there is no way to know why it works the way it does.
Good answers. Only want to add that Eclipse (the one I use often) has templates, which you can modify in order to generate your code the way you want. It could help to adjust the code to your needs.
PS. I rather use setters and getters. Just as an habit, keeps the code coherent, I feel like it will be easier to read for someone else if I keep the habits all over the code.
You should never call a non-final method from a constructor. A class constructor is used to initialize an object, and the object is not in a consistent state until the constructor returns. If your constructor calls a non-final method which is later overridden by a subclass, you can get strange, unexpected results because the object is not fully initialized when the overridden method is called.
Consider this contrived example:
The output of this program is:
The reason is that
B
'snumber
member is not initialized at the timesetX
is called, so its default value of0
is used.This article has a more thorough explanation, as does Effective Java.
Constructors are for initialization. Initialize private instance variables directly in the constructor. Methods define an object's behavior. Behavior occurs after instantiation/initialization. Manipulate the state of your instance variables with your setter methods. That's classic OOP thinking and probably why your IDE is generating the code it does.
That depends. If your setters/getters are simply accessing the members you should access them directly. If you also have some code along with it, use setters.
First of all
initialization != setters
(at least not always)But IDEs are just playing nice with once revered JavaBean design pattern Assuming property changes should happen via setters.
So, it is a matter of design. If your classes represent pure value objects, no harm in initializing via
=
But if your classes have a potential of becoming aJavaBean
whose property change is more than just init or assignment, go withset*
calls.