This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
Eric Pramono
Ranch Hand
Joined: Jul 09, 2001
Posts: 74
posted
0
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
Bob Vel
Greenhorn
Joined: Mar 21, 2001
Posts: 24
posted
0
Hi Eric, Thanks for your reply. It is not clear yet. How come when you use curly braces it is working fine. I am still confused.
Eric Pramono
Ranch Hand
Joined: Jul 09, 2001
Posts: 74
posted
0
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
Ragu Sivaraman
Ranch Hand
Joined: Jul 20, 2001
Posts: 464
posted
0
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
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
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
Desai Sandeep
Ranch Hand
Joined: Apr 02, 2001
Posts: 1157
posted
0
Bob, The following code snipplet will compile :
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)
<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Bob Vel
Greenhorn
Joined: Mar 21, 2001
Posts: 24
posted
0
Thank you all for clearing my doubt.
Kaleem Haqqi
Greenhorn
Joined: Aug 01, 2001
Posts: 22
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.