About the initialize() method in a DataField class

Hello:

I have these two situations:
classmyFieldView extends WatchUi.DataField
{
var myvar=10;
function initialize()
{
DataField.initialize();
}

and
classmyFieldView extends WatchUi.DataField
{
var myvar;
function initialize()
{
DataField.initialize();
myvar = 10;
}


Are them equivalent, i. e., can I safely initialize the variable myvar at the moment of its declaration outside the initialize() method? The variable value can be change during the Data Field execution. If I initialize it inside the initialize() method, must be the assignation after the DataField.initialize(), am I right?
  • Functionality wise, they are the same, but I'd probably go with the first (var myvar=10; ) and then change it if needed in initialize. More of a style thing though. Same with having DataField.initialize(); as the first thing in initialize().
  • They are indeed very similar, but not exactly the same. In the first case, myvar will be initialized before the myFieldView.initialize() is called. In the second case it will be initialized to null before myFieldView.initialize() is called, and will be set to 10 inside.

    In languages that allow multiple constructors/initializers, the former syntax is a good way to avoid duplicating code to initialize a member the same way without duplicating a bunch of code. Since MonkeyC only allows one initializer this isn't really a concern.

    So, yes, as Jim has said above how and when you initialize the members is a matter of style. There may be cases that it matters, but for the most part it is a style thing.

    Travis