0

I am new to Java and and not sure how to do this correctly.

I have a String variable textMain and I would like to pass it into a new object TextToSpeech. Is it possible? If so, how to do it?

I have to declare this variable outside of the object, unfortunately this object does not 'see' this variable.

String textMain = "text text";
textToSpeechSystem = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            speak(textMain); // textMain doesn't visible
        }
    }
});

Sorry if I wrote something wrong, I don't know the correct nomenclature yet.

3
  • 1
    If you want textMain to be closed over, it needs to be final. Commented Oct 18, 2020 at 18:19
  • This is it, great, thanks a lot! Commented Oct 18, 2020 at 18:34
  • Does this answer your question? Difference between final and effectively final Commented Oct 18, 2020 at 19:14

3 Answers 3

1

Your object you want to pass the string needs to have a field to store the value

Let's say that you have a class TextToSpeech with a constructor that has a string parameter to set the value at object creation.

public class TextToSpeech {
  private String textMain;
  ...

  public TextToSpeech(String text, ...) {
    textMain = text;
    ...
  }
}

Or you can have a setter method in order to set the value after object creation

public void setText(String text) {
  textMain = text;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Finally I adde just like said oleg.cherednik, befor my variable final
1

Any time you are referencing a local variable in an anonymous class / lambda you need to declare that variable as final (immutable).

final String textMain = "text text";
textToSpeechSystem = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            speak(textMain); // textMain doesn't visible
        }
    }
});

Comments

-1

Thats because TextToSpeech.OnInitListener and textMain has different location in memory: TextToSpeech.OnInitListener is been located in the heap and available after current context will be closed, but textMain is been located in the stack and not available after current context will be closed.

To fix it. all you have to do is to move textMain to the heap.

final String textMain = "text text";

1 Comment

How does using final make it move to the heap? Even without the final keyword, the string object itself will be stored in the heap, which the reference will be stored on the stack. Using final makes sure that post variable capture, the value of the captured variable does not change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.