Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance variable initialization  RSS feed

 
Yosi Hendarsjah
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When initializing instance variables, should we set the initial values when we declare them or should we set them in the constructor?

or
 
Brian R. Wainwright
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yosi,
Well... it depends. If you wish the values of your instance variables to be publicly accessible, then it's a better practice to make them private members of the class and provide accessor and mutator (aka getter and setter) methods for them so other classes, including the class itself, can use them. Also, I'd provide two constructors. One default constructor that takes no arguments, and a second constructor that takes arguments for setting instance variables.

With classes that have a large number of instance variables however, having a second constructor that takes a lot of arguments can be cumbersome. Instead it's better to identify those values you wish to have initialized when the object is constructed and use the setter methods for all the others. Hope this helps.
---BW
[ December 02, 2003: Message edited by: Brian R. Wainwright ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When initializing instance variables, should we set the initial values when we declare them or should we set them in the constructor?
As Brian alluded to, it's often a matter of personal and/or organizational preference.
If an organizational coding style guide dictates that instance variables are to be initialized in constructors, then that would pretty much resolve the question.
A lot of times, I find it nice to have variables initialized when they are declared. That way I don't have to go searching for all possible initializations to understand what some variable might actually refer to.
Of course, it's definitely appropriate that an instance variable is initialized in a constructor, if that constructor takes as an argument some value that is to be used to initialize said instance variable.
In situations where a class defines multiple constructors that should initialize some instance variable, then sometimes it's nice to make use of constructor chaining, where the actual assignment statement used to initialize some instance variable occurs in only one constructor. I find that this can help a lot when trying to avoid the problem of tracking down all possible initializations of some variable, as mentioned above, and when some rules (such as precondition assertions) are involved in the instance variable's initialization, because then the rules can be implemented in a single location.
[ December 02, 2003: Message edited by: Dirk Schreckmann ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!