This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

scope of a variable  RSS feed

 
Khushbu Ghodasara
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone ,
I'm new to JavaRanch. I have a small problem. I am definitely mising something.



Here I'm getting a compile time error

Test1.java:8: variable out might not have been initialized
System.out.println("out : " + out);
^
1 error


as much as I know , life spam of i is limited to for loop but I should be able to access "out" variable outside the for loop also since it has been defined in the main.
Now if I change the above code a little bit like this....



then its working fine..... I'm not able to figure it out why?

will appreciate your help.

Thanks.
 
Khushbu Ghodasara
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the trouble..... but I figured it out (I think),
if the variable has been defined in a method then it has to be declared also because in that case it will not be assigned any default value by JVM.

If the variable is a class variable then in that case even if we don't assign any value to it, JVM will assign the respective default value....

but still if you confirm this it will be better....

Thanks again.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the problem is that the compiler is not smart enough to know that your for loop will always be executed. it just sees that "hey, if somehow we never enter the for-loop, out will not be initialized".
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and yes, member variables, once defined for the class, do get initialized automatically, even if you don't do it yourself.

local variables to a method MUST be explicitly initialized. ALL possible paths of execution must end up initializing them. So, if your for loop doesn't execute, your 'out' does not get initialized.

the scope of 'i' is not relevant here.
 
Khushbu Ghodasara
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if I declare



like in the second code it is giving me proper output like

 
Oliver Kamps
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can certainly access variable "out" outside the loop--and the compiler is not complaining about that.

"out" is a local variable and therefore not initialized when you define it. You only assign a value to "out" inside the loop. If the code inside the loop is not executed, the variable remains uninitialized. The compiler does not evaluate the loop-condition, so for all it knows, variable "out" may be uninitialized. And that is exactly what this error is about.

Cheers,
Oliver
 
Khushbu Ghodasara
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Fred.
 
Khushbu Ghodasara
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Oliver,

for loop is being executed once and at that time

out = i;

will be executed anf after that break statement will be executed and control will come out of the loop so in that case out should be assigned value 1. If we look at both the code the only difference is

code 1 : int out;

code 2 : int out = 0;


second code is working fine and printing the value
out = 1;
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for loop is being executed once and at that time
You and I both know the for loop, as written, will be executed.

HOWEVER, the COMPILER is dumb. it is not NEARLY as smart as you and me. It reads your code, and says "Ok, I have this variable called 'out'. does it get intialized? lemme check... well, if we go into the for-loop, it does. that's good. but what happens if the for-loop ISN'T executed? I'm too dumb to know if the loop will run or not, so I better make sure 'out' gets initialized somewhere else... Oh NO!! If the for-loop doesn't run, 'out' will NOT be initialzed. I can't let that happen!!! I'll stop compiling, and tell everyone that 'out' may not always be intialized."

In your second example, with the int out = 0;, the compiler says "yep, out will be initialized NO MATTER WHAT, so it's good to go!!!".
[ May 23, 2007: Message edited by: Fred Rosenberger ]
 
Khushbu Ghodasara
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh oh, Now I got it.....

Compiler was not going to the next statement after int out; So that means I can not initialise method variable later some where in the method, I have to initialise it where I declare it.

Thanks a lot, Fred.
 
Khushbu Ghodasara
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok again I was wrong, now this time, I hope I am getting to the right point ,

We can initialise method variable later also but if we are initialising it during any loop and using it out side the loop, then compiler will think, we have not initialised it explicitly and will not compile the code ,

I tried some code and I think now, I've got it right
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're getting it now. think about it this way... what if your code looked like this:
if the user enters 0, the for-loop never executes. so, out is never initialized. The compiler obviously CANNOT know what howManyLoops is, since it will be determined at runtime. It does know that the body of a for loop may or may not run. in this sample, if the loop doesn't run, out is not initialized. That's a problem so the compiler stops compiling.

it's the same thing with your code. the for loop may not ever run (as far as the compiler is concerened), so it bails out.

when you explicitly declare int out = 0;, then you are garanteed to have out initialized. problem solved.

it is almost always a good idea to intialize your variable when you declare them.
 
Khushbu Ghodasara
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, finally I got it right....

Thanks Fred
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!