my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor question" Watch "Constructor question" New topic

Constructor question

Bob Vel

Joined: Mar 21, 2001
Posts: 24
1 class Test {
2 Test(int i) {
3 System.out.println("Creating Rock number "+i);
4 }
6public class SampleConstructor {
7 public static void main(String[] args) {
8 for(int i = 0; i < 10; i++)
9 new Test(i);
10 }
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
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

Joined: Mar 21, 2001
Posts: 24
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
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
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.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
r balasubramanian

Joined: Jul 25, 2001
Posts: 11
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.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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,
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
Bob Vel

Joined: Mar 21, 2001
Posts: 24
Thank you all for clearing my doubt.
Kaleem Haqqi

Joined: Aug 01, 2001
Posts: 22
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:
subject: Constructor question
It's not a secret anymore!