wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan Question - doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Question - doubt" Watch "Dan Question - doubt" New topic
Author

Dan Question - doubt

Agathian Thangavelu
Greenhorn

Joined: Sep 21, 2002
Posts: 8
Hi all , Help mw following code.
class A {
public static void main (String[] args) {
int i = 0,j = 0,m = 0,n = 0,p; <<<<------
label1:
try {
do {
m++;
try {
p = i + j + m + n;
if (p>=5) break label1;
n++;
} finally {i++;}
} while (m++ < 2);
} finally {j++;}
System.out.print(i + "," + j + "," + m + "," + n);
}
}

Above code, No need of initilize the local varible p?
I thought it will compiler error,
Thanks in Advance,
Aga
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Moving this to the Prog Cert forum. Please continue the discussion there.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
I think it gives compiler error only if you try to access it before initializing it. Here you are initializing it and then trying to use it so it should work fine.


SCJP 1.4<br />SCWCD 1.3
Ruff Young
Ranch Hand

Joined: Sep 26, 2002
Posts: 513
In your case, it seemed that the variable 'p' didn't initialized at first. However it shows that 'p' is initialized as "p=i+j+m+n" prior to using it in the main block.
When you declare a variable in main or method block, you have to initialize it before using it in main or method block. Otherwise the compiler will complain it.

//
...
p = i + j + m + n; // initialize
if (p>=5) break label1; // This line is first time using the variable 'p'
...
//
One more thing, although you declare a variable 'x', it will compile ok if you don't use it at all.


SCJP2, SCWCD<br />IBM Certified WebSphere<br />IBM Certified Solution Developer for WSAD
Agathian Thangavelu
Greenhorn

Joined: Sep 21, 2002
Posts: 8
Thanks all, Now i understand.
Aga
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan Question - doubt
 
Similar Threads
Label control doubt??
Switch/Label
a question from Dan's topic mock exam
compile time error
bit values of an integer