I’m just wondering why I get a “cannot resolve symbol initialText” with the following code. isItA is a boolean which the user selects earlier depending on which type of text (A or B) they are about to paste into the text area. There are 2 potential strings for initialText, and the one which is outputted depends on the boolean isItA. I dont get why this error pops when i use initialText outside the if/else statement; if is inside the ‘if/else’ statement, the compiler should be able to resolve the string outside right? I’m currently using GWT in IntelliJ. At this point I am still sort of noob at java, so a basic explanation of why this is happening would be greatly appreciated 🙂 Thanks in advance. A snippet of the code is below.
protected TextArea getNewTextArea() {
if ( newTextArea == null ) {
if (isItA){
final String initialText = "Please paste valid text A here, and then
press" + "the \"" + Labels.ADD_A_BUTTON_TEXT + "\" button.";
}else{
final String initialText = "Please paste valid text B here, and then press " + "the \"" + Labels.ADD_B_BUTTON_TEXT + "\" button.";
}
newTextArea = new TextArea();
newTextArea.setText( initialText );
newTextArea.addClickHandler( new ClickHandler() {
public void onClick( ClickEvent clickEvent ) {
// If the text is still the original text, then clear it.
if ( newTextArea.getText().equals( initialText ) ) {
newTextArea.setText( "" );
}
}
});
}
}
initialText is unresovlable because it is not within the given code block scope.
To fix this, declare intitalText before the if/else statement like so: