S
Shawn
Hi,
I found an error very annoying. I am hoping someone can give me some
suggestion.
public MyClass getMyClass
{
MyClass myClass = null;
//opens a gui for the user. Normally, the user clicks the button
//and the object myClass gets created. But if the user clicks
Cancel button, then myClass will not be created
if (....) //user clicks the button
{
myClass = new MyClass(..); //myClass object created here
}
else //user clicks the Cancel button
{
System.out.println("User has cancelled the command");
}
return myClass; //NOW, my compiler won't let me go. It says the
local variable myClass may not have been instantiated.
}
instead, the following code works:
public MyClass getMyClass
{
MyClass myClass = null;
if (....) //user clicks the button
{
myClass = new MyClass(..); //myClass object created here
...
return myClass;
}
else //user clicks the Cancel button
{
System.out.println("User has cancelled the command");
return null;
}
}
It ends up with two returns, which I don't like. I don't understand why
I cannot pass with the first code.
Thank you very much.
I found an error very annoying. I am hoping someone can give me some
suggestion.
public MyClass getMyClass
{
MyClass myClass = null;
//opens a gui for the user. Normally, the user clicks the button
//and the object myClass gets created. But if the user clicks
Cancel button, then myClass will not be created
if (....) //user clicks the button
{
myClass = new MyClass(..); //myClass object created here
}
else //user clicks the Cancel button
{
System.out.println("User has cancelled the command");
}
return myClass; //NOW, my compiler won't let me go. It says the
local variable myClass may not have been instantiated.
}
instead, the following code works:
public MyClass getMyClass
{
MyClass myClass = null;
if (....) //user clicks the button
{
myClass = new MyClass(..); //myClass object created here
...
return myClass;
}
else //user clicks the Cancel button
{
System.out.println("User has cancelled the command");
return null;
}
}
It ends up with two returns, which I don't like. I don't understand why
I cannot pass with the first code.
Thank you very much.