Original Post
Hi all, This is a pretty basic problem but I'm not used to Java... C++ doesn't force such correctness issues on you! I have some code which (in abstract form) looks something like the following: At the call to myComponent.doSomething() Java complains that my variable might not of been initialised, presumably because the exception might have been thrown. But in this case i'll have exited and won't care. In other words *I* know it's initialised, even if Java doesn't. Whats the 'correct' approach to this? Should i declare the Compoment with: Component myComponent = new JPanel(); so it's always initialised to something? This is clearly a waste of time as it will get reassigned in the next line anyway. Or does my 'Whole lot of code here' all have to go in the 'try' block. I love Java but just wish it trusted me a bit more!
Component myComponent;
try
{
//Initialise my component...
myComponent = funtionWhichReturnsComponentSubclass();
}
catch(Exception e)
{
//Scream lots and die
System.exit(1);
}
//Whole lot of code here...
.
.
.
myComponent.doSomething();