my dog learned polymorphism
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Question - doubt" Watch "Dan Question - doubt" New topic

Dan Question - doubt

Agathian Thangavelu

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; <<<<------
try {
do {
try {
p = i + j + m + n;
if (p>=5) break label1;
} 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,
Valentin Crettaz
Gold Digger

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

[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

Joined: Sep 21, 2002
Posts: 8
Thanks all, Now i understand.
I agree. Here's the link:
subject: Dan Question - doubt
It's not a secret anymore!