Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor question

 
Bob Vel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
r balasubramanian
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Bob Vel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for clearing my doubt.
 
Kaleem Haqqi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic