This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor question" Watch "Constructor question" New topic
Author

Constructor question

Bob Vel
Greenhorn

Joined: Mar 21, 2001
Posts: 24
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
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
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.
HTH
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
Greenhorn

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.
R.Balasubramanian
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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
Thank you all for clearing my doubt.
Kaleem Haqqi
Greenhorn

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: http://aspose.com/file-tools
 
subject: Constructor question
 
Similar Threads
just check this...
Doubt in assertion
Why final object value can be change whereas final variable value can't?
Interface instant variables
Still unable to call a method