0

So I'm trying to declare an integer variable inside an if/else statement, and print it out, outside of it. Something like this:

int x;
int a = 1;
if (a == 1)
{
int x = 5;
}
System.out.println(x);

This is just an example of what I'm trying to do, as I don't have the actual code with me, and i don't want to redo it all over. Although it shouldn't matter really, as the example is exactly what i need, only with different variable values and names (but it's still an integer). At first i just declared and initialised the variable inside the if/else statement, but then I was told I need to declare it outside the statement... So I did that, then initialised it within the statement, and then proceeded to call on it later on. However I'm still getting an error, either it says the variable hasn't been initialised, or if I assign a value to it (x) then update it inside the statement, the error i get is that it has already been declared. Any help would be appreciated, thanks.

Unknown
  • 25
  • 1
  • 8
  • `At first i just declared and initialised the variable inside the if/else statement but then I was told I need to declare it outside the statement` because the scope of variable `x` ends inside the `if` – sam Oct 15 '15 at 13:08

5 Answers5

1

Yes. Local variables needs to be initialize before they use. Where as instance variables initialize to default values if you didn't initialize them before use.

If you are curious about the reason? click here to know

Coming back to your question again,

Because consider the below scenario

Follow comments.

int x;  // declared with no value 
int a = 0;
if (a == 1)  // this is false
{
 x = 5;  // this never executed 
}
System.out.println(x); // what you are expecting to print here ?

Hence you need to initialize with a value. For ex : initialize it with zero and change it later on based on a condition

int x=0;
int a = 1;
if (a == 1)
{
 x = 5;
}
System.out.println(x);
Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0
int x = -1;
int a = 1;
    if (a == 1)
    { // here begins inner 'area' for declared variables
     x = 5;
    }// and here ends
    System.out.println(x);

OK, my bad! I wanted him to wonder why and try some other ways of writting it and letting him get 'hit' by IDE errors. So Mr. Unknown as far as you declare variable 'inside' if statement it is visible only across that statement! So basicly if You want to do something with variable inside if statement, and have results outside of it, You need to declare it outside the statement making it having wider range of accessibility! If You have any questions don't hesitate to ask ;)

P.S. Watch out for re-declaring variable with the same name like You tried to do here, it's nasty bug to find =)

Fincio
  • 198
  • 8
  • Thank you :D See, I knew Im supposed to declare it outside the statement, but i didnt know i am not supposed to have the 'int' prefix before the 'x'..... I'm new to java :D – Unknown Oct 16 '15 at 09:47
0

The point is that you declared x above. So remove int before x inside the if-statement. Then it works.

int x; 
int a = 1; 
if (a == 1) { 
    x = 5; 
} 
System.out.println(x);

Implicitly an integer is initiated with 0. If you want to be sure just write

int x = 0;

your code here
D3myon
  • 181
  • 1
  • 10
0

Simply assign the int x = 0 before your if-statement, then instead of redeclaring x as an integer equal to 5, set x equal to 5 inside your if statement.

MrPublic
  • 520
  • 5
  • 16
0

Thank you all for the answers, I realized I made 2 minor mistakes that didnt allow it to work, I (in most attempts) didn't declare a value for x before the if statement, and I had 'int' in front of x inside the if statement, which caused the re-deceleration error. So yea, thank you for the quick answers :)

Unknown
  • 25
  • 1
  • 8