Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

why does it happen?

807600Sep 9 2007 — edited Sep 9 2007
i have two cases to analise tell me why so happens
void method()
{
Integer i ;
try{ i = new Integer(8);
}catch(Exception e){;}
System.out.println(i);
}
// generates an error that object might not be initialised now as far as i know i is object whose reference is outside try block than why does it get lost.
Interger i;
void method()
{
i = new integer(9);
}
void anotherMethod()
{
System.out.print(i);
}
// in the above case no such problem occurs

Comments

807600
Objection!!! Asked and answered:
http://forum.java.sun.com/thread.jspa?threadID=5214381&messageID=9867476#9867476
807600
A gold star i see ... :-) congrats, but please do not adopt an elitist/pompous attitude that many display here.
800322
A gold star i see ... :-) congrats, but please do not
adopt an elitist/pompous attitude that many display
here.
Believe me, we had that attitude years before those stars were invented.
807600
A gold star i see ... :-) congrats, but please do
not
adopt an elitist/pompous attitude that many
display
here.
Believe me, we had that attitude years before those
stars were invented.
Yep... nothing to do with the stars; everything to do with the knowledge that being even a poor java developer is a thousand times better than being the best .NET, VB, C++ developer around.

[edit]
I hadn't even noticed until you pointed it out. When did that happen?
[edit]
807600
Believe me, we had that attitude years before those
stars were invented.
Yikes. Must be hard to work around you guys, no offense.
807600
[edit]
I hadn't even noticed until you pointed it out. When
did that happen?
[edit]
If you are referring to your star, not sure probably just now :0).
807600
hey my question is not yet answered, i went to that link and the link u had given in it but it talks about scope and its rules but i did not find my questions answer even there
807600
hey my question is not yet answered, i went to that
link and the link u had given in it but it talks
about scope and its rules but i did not find my
questions answer even there
The answer is: scope. That's your problem. When you understand the concept of scope, you will understand what your compiler is telling you. Your variable is declared outside the scope in which it is initialized. Since it is in a try/catch block, there is a chance that the initialization will not be reached. The compiler doesn't know for sure that it will so it is throwing that error.
800322
In short: a variable only exists inside the set of curly braces it's declared in.
807600
The answer is: scope. That's your problem...........
So the link was useful after all?
796365
The answer is: scope. That's your
problem...........

So the link was useful after all?
No, because it did not give code that s/he could cut-and-paste :)
dcminter
The answer is: scope. That's your problem.
I'm not so sure. According to the original post, "i have two cases to analise" which sounds exceedingly unpleasat. It's a filthy thing to do to perfectly good scotch.
1 - 12
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 7 2007
Added on Sep 9 2007
12 comments
66 views