Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why variable declaration fails in IF statemenet

Status
Not open for further replies.

Guy1978

MIS
Apr 21, 2005
7
BR
As we all know, in Java you can combine an array declaration with array initialization, as follows:
Code:
n1 = 5;
double[] g = new double[n1 + 1];
But why is it that such a statement will not work if declared within the bounds of an IF statement? I had never heard before that variables cannot be declared within if statements! The code segment is as follows:
Code:
n1 = 5;
n2 = 7;
if (n1 > n2)
    double[] f = new double[n1 + 1];
else
    double[] f = new double[n2 + 1];
The code returns a compile error for both lines, saying:
".class expected" and "not a statement" for both of the array declarations/initializations. Could someone tell me why?
Thanks
 
It does work... But the scope is the if statment.
Code:
if(codition)
{ 
  int i[200] = {...}; //i comes to exsist here
  i[1] += i[2];
} //ends exsistance here.
//i doesn't live here

Much like the scope of a try/catch, sychronize or function.
The way to do this is (with Objects, istead of primitives):
Code:
String s[];
if(condition)
 s = String[200];
else
 s = String[300];

[plug=shameless]
[/plug]
 
Just enclose it

Code:
int n1 = 5;
int n2 = 7;
if (n1 > n2) {
    double[] f = new double[n1 + 1];
} else {
    double[] f = new double[n2 + 1];
}

Cheers,

Dian
 
Hmmmm...what both of you said is interesting. I thought that the reference variable f would only be invisible outside the IF statement (and hence create a scope problem) if it were declared within braces. In fact, when I did as Dian suggested, the compiler no longer complained of ".class expected" or "Not a statement"; only later in the program, when f is passed as a parameter, did the compiler complain that f had not been declared (it was out of scope). So Dian's suggestion did make the ".class expected"/"not a statement" problem go away.

Jstreich, declaring the reference variable before the IF statement solved all the problems. But why did I get ".class expected"/"not a statement" for the variable declaration inside the IF statement. If it truly were a scope problem, would the compiler not have complained (at some other point in the program) that f had not been declared? Would I not have gotten the same compile error I got when I enclosed the variable declarations in braces?

Thanks

 
I think the IF statement requires some sort of executable expression after it. The variable declaration isn't considered an executable expression, hence your errors.

However, braces all by themselves ARE considered a valid expression. That's why you can just put {} after an if statement.

So, I don't think enclosing the declaration in braces made the error go away. I think just the existence of the braces did the trick - they fulfilled the requirement for an executable expression.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top