I have a question about instance variables initialization. Hi have the following code
My doubt is why, even though I'm initializing the 'var1' instance attribute at the declaration point, I get a null at the time the method 'printOutput' is run?
Initialization of type 'var1' is made through a static method ('getType' in the example) in another class from another package.
If I initialize the 'var1' attribute in the same 'printOutput' method just before printing it works fine.
Well, in fact there is no other code that changes the value of the instance attribute. The attribute is declared and instantiated in one line and in a private method called from the constructor of the class a method of the 'var1' attribute is called. Is there any restriction or problem in that sequence?
Why don't you just show us the real code? Or at least a short version of the real code which actually exhibits the problem? Looking at a bogus code fragment where you assert various things about the code you didn't post is not very practical. Show us the code.
I'm sorry for leaving this thread unattended all these days but I was ill and with a lot of work afterwards.
Anyway I have solved the problem. It was all because of the way constructors were called. Constructors were not finished initialising when I intended to use an instance attribute still not initialised.
In the end it was java basics, but something I did not see at fisrt.
Here is the code so you can comment on it and give me your opinion.