How to change variable value throughout outer class when it changed in an inner one?

1.1k views Asked by At

I have a swing class that includes a String variable str3 declared as final and two

ActionListener interfaces that implemented by two JButtons b1

and b2 , when b1 JButton is pressed str3 String takes a value ,

My question here how to make str3 value to be changed throughout the class

rather in the second ActionListener interface (not in the first inner class only ) .

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class mySwing extends JFrame {

    JButton b1, b2;

    public mySwing() {
        final String str3;
        JPanel panel = new JPanel();
        b1 = new JButton("please click me first");
        b2 = new JButton("please click me second");
        final JTextField txt = new JTextField("                            ");
        panel.add(txt);
        Container pane = getContentPane();
        panel.add(b1);
        panel.add(b2);
        pane.add(panel);
        str3 = new String();
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent acv) {
                String input = "HelloWorld";
                String str3 = new String(input.substring(0, 5));
                txt.setText(str3);
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent acv) {
                txt.setText(str3);
            }
        });
        setVisible(true);
    }

    public static void main(String[] args) {
        new mySwing();
    }
}
3

There are 3 answers

3
Holger On BEST ANSWER

Just make str3 a non-final instance variable of your outer class mySwing.

By the way, do not do things like new String(input.substring(0, 5)) the result of input.substring(0, 5) is a String so you don`t need to create another String.

Based on your code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class mySwing extends JFrame {

    JButton b1, b2;
    String str3="";

    public mySwing() {
        JPanel panel = new JPanel();
        b1 = new JButton("please click me first");
        b2 = new JButton("please click me second");
        final JTextField txt = new JTextField("                            ");
        panel.add(txt);
        Container pane = getContentPane();
        panel.add(b1);
        panel.add(b2);
        pane.add(panel);
        str3 = new String();
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent acv) {
                str3+=" (1)";
                txt.setText(str3);
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent acv) {
              str3+=" (2)";
              txt.setText(str3);
            }
        });
        setVisible(true);
    }

    public static void main(String[] args) {
        new mySwing();
    }
}
1
Martin On

Your approach of declaring the variable str3 as final is correct. However, in Java Strings cannot change their contents, so you would have to do something different. Some ideas I can come across:

  1. Use a StringBuffer instead, and convert it to a String with the toString() method where needed.
  2. Use some other POJO with a getter and setter for your String, this way the POJO will be declared as final and the content can be changed via the getter and setter.

Hope this helps as a guideline, there sure are many other good approaches.

2
Wagdi Kala On

your variable str3 is a final, then it can never be changed.

inside your code you've never tried to change it, you just declared a new str3 inside your ActionListener inner class.