Unable to public a string

Viewed 34

I want to call a string in a different class but I am unable to make it public it's showing Modifier 'public' not allowed here.

public void onAccessibilityEvent(AccessibilityEvent event) {
        switch (event.getEventType()) {
            case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:

                String string = event.getText().toString();

I want to call the string in a different class in onStartCommand method as for if statement kindly guide me on how can I call the strig without public, or any other method to achieve it.

2 Answers

I would suggest to put the declaration public String string; outside your method (for example right beneath your class declaration). You can also declare it private (in the same position though) and add a public getter method:

public String getString() {
    return string
}

You can't make him public, because you're declaring him inside block of code of a method. So declare the variable string as global variable then your can make it public and using it in another classes. Like this :

public String string;
public void onAccessibilityEvent(AccessibilityEvent event) {
    switch (event.getEventType()) {
        case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:

            string = event.getText().toString();
Related