I have the code at the bottom of this post:
Running abc x = new abc()
can often take a long time (minutes or longer) and is only ever necessary if flag is true.
If I leave the code as it is below, I get a "x cannot be resolved error" relating to the x.method()
line. As you can see, this is only ever run if flag = true (and flag is only ever set to false, within an if statment in this) - i.e. There would never be a time where x.method()
is called, without x being initialised first.
I can resolve this by removing the first if statement, and running abc x = new abc();
every time the software is run, but as mentioned this can be very slow, and is not always necessary.
I understand why java doesn't like me doing what I'm trying (it can't see the semantics of what I'm doing, to ensure it gets a value for "x") but I was wondering if there is any way to override this, or a better way of doing what I'm trying to do?
boolean flag = //(input from user)
if(flag)
{
abc x = new abc();
}
do
{
if (flag)
{
if (x.method ())
.....
{
else
{
.....
}
}
while(...)