• 0

[JAVA] Conditional object definition


Question

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

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

If you declare a variable inside a {} block, when you exit that block, the variable is destroyed.

The way to do this would be:

public class sample {

 ? ?public static void main (String[] args) {

 ? ? ? ?Test test = null;
 ? ? ? ?if (1 == 1) {
 ? ? ? ? ? ?// This will always run
 ? ? ? ? ? ?String input = "nothing";
 ? ? ? ? ? ?test = new Test(input);
 ? ? ? ?}

 ? ? ? ?test.method(); ?// Will crash if the 'will always run' part doesn't!
 ? ?}
}

Link to comment
Share on other sites

  • 0

This looks like bad code in general -- why do you want the program to crash? Also, the java compiler may still complain with this (perhaps when 1 == 1 becomes something else) because null is not a class instance, and therefore will not have the method() method.

Link to comment
Share on other sites

  • 0
Hmmmmm if you want the if condition to always be processed you probably should just do if(true) as opposed to the test condition.

584835979[/snapback]

I don't even get why he needs that conditional if it's always true :huh:

Link to comment
Share on other sites

  • 0
I see another problem. If the method contains a parameter in its signature shouldn't you pass an argument when you call it?

Maybe I'm wrong.

584837011[/snapback]

It won't compile... but hey you can't elliminate the possibility of the method being overloaded.

Link to comment
Share on other sites

  • 0

The guy made a quick example to explain what he meant. I'm sure he doesn't try to use that code.

Now, he had his answer, maybe we can let this thread die..?

Link to comment
Share on other sites

  • 0
It won't compile... but hey you can't elliminate the possibility of the method being overloaded.

584837019[/snapback]

Yeah that is true however according to his explenation and the code he showed us, does not seem to be overloaded, he only shows one "method" method.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.