clarify the doubt about accesing instance variable and local variable
if we use instance variable(without initialization or assigning any value) compiler won't give any error,but in case of local variable compiler gives error,
i know that instance variables initializes with default values but local variables won't..
why it is so...
Re: clarify the doubt about accesing instance variable and local variable
How do you define local variables?
Is this;
??? If yes, then it's not local variable but primitive type or compile time type variable. And it's auto initialized by it's default value, that is 0
Re: clarify the doubt about accesing instance variable and local variable
Hi,
Can you show your code.
Core java
Re: clarify the doubt about accesing instance variable and local variable
Quote:
Originally Posted by
Mr.777
How do you define local variables?
Is this;
??? If yes, then it's not local variable but primitive type or compile time type variable. And it's auto initialized by it's default value, that is 0
That's not actually true. Local variables (variables inside methods) do not get automatically initialized like class variables do. That's the source of the "variable may not have been initialized" compiler error. And I'm not sure what you mean by saying that it's not a local variable but a primitive type- local variables can hold primitive types, but they certainly don't have to. Not sure what you mean at all.
And to the OP- Your question is why is this the case? The short answer is because that's how the designers of Java made it. You can argue for or against it all you like, but it's simply true. And any argument for or against it is mostly guesswork anyway. Best just to accept it and move on.
Re: clarify the doubt about accesing instance variable and local variable
Quote:
Originally Posted by
mr.miku
Hi,
Can you show your code.
What code? He's asking about a general, obvious concept. I'm all for showing code, but in this case, it's not necessary.
Re: clarify the doubt about accesing instance variable and local variable
It is called Definite Assignment. Read the following links (especially the motivation section in the second link)
Definite Assignment
Definite assignment analysis - Wikipedia, the free encyclopedia