1 class Test { 2 Test(int i) { 3 System.out.println("Creating Rock number "+i); 4 } 5} 6public class SampleConstructor { 7 public static void main(String[] args) { 8 for(int i = 0; i < 10; i++) 9 new Test(i); 10 } 11} The above code works fine. If I change line number 9 to Test r = new Test(i); I am not able to complie code. Why?
Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.<BR>No involvement, no commitment.
Hi Bob, That's an interesting one. I've never tried that one.. Well, apparently the compiler will declare it as "Invalid declaration". You would have to put the curly brackets "()", e.g.:
I've tried with a number of other variable-types' declarations, and it always gave me the same result whenever I miss that curly bracket. I guess that's how the compiler works. If it sees a variable declaration straight following the for loop then it'll complain, unless we give the curly bracket beforehand. please anyone, correct my assumption if it's wrong. - eric
Hi Bob, According to JLS 2.2 Grammar Notation For Statement for (ForInit; Expression; ForUpdate) Statement It seems like the variable declaration is not a statement with regards to the compiler. please also refer to JLS 14.2 Blocks it seems like LocalVariableDeclarationStatement is a different thing from Statement itself. please anyone correct me if I'm wrong. - eric
We cant declare a variable inside a loop as a single statement. But we can assign a value to a variable. If you wanna do both, then they are two different set of instructions to the compiler. So in the loopbody they become as block of statements. Hence we need a curly braces. HTH
JLS §14.2 states that Every local variable declaration statement is immediately contained by a block. I think the problem, when curly braces are omitted after a <code>for</code> statement, is that the compiler can't tell which block the code belongs to. A <code>for</code> statement implies a block but without curly braces the intention of the statement <code>Test r = new Test(i)</code> isn't clear. Which block does it relate to? The one implied by the <code>for</code> statement or the method block?? The compiler can't tell so it coughs up an error. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
Test r=new Test(i) is not a single statement. It is a block of statement. First we are declaring a variable and then we are assignment a value for the same. Hence, it has to be within the curly braces. R.Balasubramanian
In you earlier code snipplet you were doing declaration and initialization together.Since you cannot declare the variable with the same name again, you were getting compile-time error. Hope this helps, Sandeep SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
Dear Bob, I just complied this code in JDK1.3 compilier. Its works fine and both new Test(i) & Tesr r =Test(i) complied with no error. Let me know you have JDK 1.3??
<B>-Kaleem Haqqi</B><BR>Java is mind work. Having the right frame of mind is essential.
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...