I am trying to define an object that is used later in the program. However, the definition is in a conditional operation which will always be executed. The Java compiler complains that the object isn't defined (since its definition is "conditional"). How can I get around this, without moving the definition out of the conditional?
Here's are the basic files:
sample.java:
public class sample {
public static void main (String[] args) {
if (1 == 1) {
// This will always run
String input = "nothing";
Test test = new Test(input);
}
test.method();
}
}
Test.java:
public class Test {
public void method (String input) {
}
}
And the error:
sample.java:11: cannot resolve symbol
symbol : variable test
location: class sample
test.method();
^
1 error
Question
hurting101
I am trying to define an object that is used later in the program. However, the definition is in a conditional operation which will always be executed. The Java compiler complains that the object isn't defined (since its definition is "conditional"). How can I get around this, without moving the definition out of the conditional?
Here's are the basic files:
sample.java:
public class sample { public static void main (String[] args) { if (1 == 1) { // This will always run String input = "nothing"; Test test = new Test(input); } test.method(); } }Test.java:
public class Test { public void method (String input) { } }And the error:
Link to comment
Share on other sites
11 answers to this question
Recommended Posts