During programming i usually come across an error related to Instance Variable.
Compiler forces me to initialize the instance variable. For Example :
In above code, compiler force me to give some value to "x" , when i assign it "null" the error gets removed.
But its not that compiler force me every time, sometimes it allows me to simply define it like above code. So, can anybody tell me when it is necessary to define the instance variable & when its not ??
You are probably mistaking "instance variable" for just any variable.
Instance variables are part of the class and do not need to be explicitly initialized. Variables inside a method are not instance variables. They are called local variables.
They must be explicitly initialized before they are used. You can sometimes declare them without initializing them. But they must be definitely assigned before you can use them.
Joined: Feb 20, 2013
E Armitage wrote:You are probably mistaking "instance variable" for just any variable.
Thanks Jesper & Armitage. Sorry that was my mistake in the question. You both have valid points. but believe me i have often come across such kind of errors...maybe they may have come due to different reasons. I know the concept of local variables, class variables & instance variables.. I dont have any such example to show you right now..Whenever in future i meet with it.. will post it here ..