Cannot refer to a non-final variable (JButton)

Status
Nicht offen für weitere Antworten.

BeSp

Aktives Mitglied
Hallo,
ich habe folgenden Code produziert, wundere mich aber dass es so nicht erlaubt ist:

Code:
JButton button1 = new JButton("Hallo");
JButton button2 = new JButton("Grunz");
				
button1.addActionListener(new ActionListener()
{
	public void actionPerformed(ActionEvent ae)
	{
		button2.setText("Verändere Mich");
	}
});

Der compiler gibt folgendes aus:
Cannot refer to a non-final variable bla inside an inner class defined in a different method

Dass diese Konstruktion nicht erlaubt ist mag sein. Auch dass man die Variablen als final bezeichnen muss damit das funktioniert.

Wie ließe sich denn dieses Problem beheben ohne final??

Gruß, G.
 

Marco13

Top Contributor
Indem man "bla" (oder button2) als Variablen innerhalb der KLASSE deklariert (und nicht direkt in der Methode). Aber - ich würde das "final" vorziehen...
 
S

SlaterB

Gast
oder eine finale Hilfsvariable verwenden;

final x = button1;

....

x.setText("Verändere Mich");

(natürlich schöner benannt als x)
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
M JavaFX Non-static method 'widthProperty()' cannot be referenced from a static context AWT, Swing, JavaFX & SWT 4
S Swing jx cannot be resolved to a variable (2.) AWT, Swing, JavaFX & SWT 1
S Swing jx cannot be resolved to a variable AWT, Swing, JavaFX & SWT 6
H Swing Spiel: Maexchen: method cannot be applied to given types AWT, Swing, JavaFX & SWT 3
Q sun.awt.X11.XException: Cannot write XdndAware property - Was bedeutet das? AWT, Swing, JavaFX & SWT 2
H Swing JDialog cannot be resolved as a typ AWT, Swing, JavaFX & SWT 12
M JTable Cannot find Symbol AWT, Swing, JavaFX & SWT 7
T The import javax.vecmath cannot be resolved AWT, Swing, JavaFX & SWT 2
K Cannot find symbol: class JCanvas AWT, Swing, JavaFX & SWT 8
A Swing LAF unter Windows: javax.swing.DebugGraphics cannot be cast to java.awt.Graphics2D AWT, Swing, JavaFX & SWT 5
M setColor mit Fehler cannot fiend symbol (java.awt.Color) AWT, Swing, JavaFX & SWT 2
S cannot find symbol. AWT, Swing, JavaFX & SWT 2
D cannot access Canvas AWT, Swing, JavaFX & SWT 13
G JTextField - cannot find symbol 114 AWT, Swing, JavaFX & SWT 4
Z Cannot load AWT toolkit AWT, Swing, JavaFX & SWT 3
J Swing The serializable class FMFrame does not declare a static final serialVersionUID field of type long AWT, Swing, JavaFX & SWT 5
B final umgehen AWT, Swing, JavaFX & SWT 1
J Anonyme Klasse - Button ansprechen - Warum muss Button final sein? AWT, Swing, JavaFX & SWT 4
J Button En-/Disablen - Warum variable final? AWT, Swing, JavaFX & SWT 2
U AWT local variable * is accessed from within inner class; needs to be declared final AWT, Swing, JavaFX & SWT 6
G Swing JButtons + ActionListener + final AWT, Swing, JavaFX & SWT 3
B problem mit variable (final) AWT, Swing, JavaFX & SWT 7
J serializable class GUI does not declare a static final s. AWT, Swing, JavaFX & SWT 2
G final JButton AWT, Swing, JavaFX & SWT 5

Ähnliche Java Themen

Neue Themen


Oben