Q:

How do I use a variable defined in an 'if' statement?

public class Help
{
    public static void main (String[] args)
    {
        if (index = 1)
        {
            String greeting = "hello";
        }
        else
        {
            String greeting = "goodbye";
        }
    }

    callAMethod(greeting);
}

When I define the String within the if statement I get a 'cannot find symbol' error. How can I get around this and still be able to create a string depending upon an above condition?

A:

How about
public static void main (String[] args){
    String greeting;
    if( index == 1){
       greeting = "hello";
    }else{
       greeting = "goodbye";
    }
 }

 callAMethod(greeting);
}

