Skip to main content
gamedev.net
🔒 Locked

[java] 'variable' might not have been initialised

Started by PolyVox Apr 11, 2005 at 4:02 PM 4 replies 1k views
Original Post
PolyVox
PolyVox
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:

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();

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!
Optus
Optus
easiest way to get around this would be to declare it null e.g.

Component myComponent = null;

edit: grammar is greater than me
TheBluMage
TheBluMage
I haven't checked, but if I remember correctly, you could also put a return in the catch block right after your System.exit call. Then the compiler should be able to figure out that there's no way it can get to the point where your component is used without it being assigned a value.
PolyVox
PolyVox
Quote:
Original post by Optus
easiest way to get around this would be to declare it null e.g.

Component myComponent = null;

edit: grammar is greater than me


So is there a difference between initialising to null and not being initialised at all? Besides, i thought in Java varialbes were initialised to null automatically...

Quote:
Original post by TheBluMage
I haven't checked, but if I remember correctly, you could also put a return in the catch block right after your System.exit call. Then the compiler should be able to figure out that there's no way it can get to the point where your component is used without it being assigned a value.


Ah, good idea! I'll give it a try.
Lotuspec
Lotuspec
Quote:
Original post by esuvs

So is there a difference between initialising to null and not being initialised at all? Besides, i thought in Java varialbes were initialised to null automatically...


Only object variables are automatically initialised

ex.

public class myClass {
private Object myVar; // automatically init to null

public myClass() {
Object someVar; // no init
}
CaptainJester
CaptainJester
Only class level variables are initialized to null.(or 0/false for primitives). Method level variables are undefined until assigned a value.
Quote:

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:

How do you know it is initialized. If that try block throws an exception, then it is not initialized. Also System.exit(1) is an API call, not a language feature, so to the compiler it is going to continue down through the code.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.